I have the following class inheritances hierarchy:First the abstract parent class:<?phpabstract class SegmentParserAbstract{/** @var ParserResult */protected $_result;protected $_invalidSegmentMessage;protected $_segmentRegex;protected $_segment;abstract protected function createSegmentObject();public function __construct() {    $this->_result = new ParserResult();}public function setSegment($Segment) {    $this->_segment = $Segment;}/** * Function will parse the Segment  by validating the input string.  * If valid it will create the Segment Object in the result array with no errors. * If invalid it will populate the result array with the error, but no object will be created.      */public function parse() {    if ($this->isValidSegment()) {        $this->createSegmentObject();    } else {        $this->createErrorMessage();    }    return $this->_result;}protected function isValidSegment() {    $matchesFound = preg_match($this->_segmentRegex, $this->_segment);    return ($matchesFound > 0);}protected function createErrorMessage() {    $this->_result->addError(array($this->_invalidSegmentMessage . $this->_segment));}}?>The first child:<?phpclass GsParser extends SegmentParserAbstract {    const SUPPLIER_ID_INDEX = 2;    const GROUP_HEADER_INDEX = 6;    protected $_invalidSegmentMessage= 'Invalid GS segment: ';    protected  $_segmentRegex=/^GS\*IN\*\w{2,15}\*\w{2,15}\*(\d{6}|\d{8})\*\d{4}\*\d{1,9}\*X\*\d{1,12}$/;    protected function createSegmentObject() {        $elements = explode('*', $this->_segment);        $gs = new Gs();        $gs->setSupplierId($elements[self::SUPPLIER_ID_INDEX]);        $gs->setGroupReference($elements[self::GROUP_HEADER_INDEX]);        $this->_result->setSegment($gs);    }}?>The second child:<?phpclass IsaParser extends SegmentParserAbstract {    const SUPPLIER_ID_INDEX = 6;    protected $_invalidSegmentMessage= 'Invalid ISA segment: ';    protected $_segmentRegex=/^ISA\*00\*.{10}\*00\*.{10}\*ZZ\*.{15}\*ZZ\*.{15}\*\d{6}\*\d{4}\*U\*.{5}\*\d{9}\*[0-1]\*[PT]\*>$/;    protected function createSegmentObject() {        $elements = explode('*', $this->_segment);        $isa = new Isa();        $isa->setSupplierId($elements[self::SUPPLIER_ID_INDEX]);        $this->_result->setSegment($isa);    }}?>Now I'll be having about nearly 16 other parsers extending the same abstract class SegmentParserAbstract with only differing in the implementation of createSegmentObject() and the number of constants (which might have different names of course).Somehow I feel that I can remove all sub classes and only keep the parent class and make it a concrete class that can be configured to work on any Segment provided it knows its regular expression and the data that needs to be extracted and the resulting object type.Any help?!
Refactoring a class hierarchy to remove subclasses
php;object oriented;design patterns