Here what I'am trying to achieve :
- this a text file parser
- depending on the first chars I create the correct parser object
- I wish to do so using the right factory pattern
- could you tell me if my code below fits correctly the factory pattern ?
- Thank you ! :-)
class Parser { protected $src; public function __construct($src) { $this->src = $src; } } class Format1Parser extends Parser { public function Parse() { // Parsing format 1 // ... } } class Format2Parser extends Parser { public function Parse() { // Parsing format 2 // ... } } class ParserFactory { public static function GetParser($src) { $header = substr($src,0,7); if ( $header == "format1" ) { return( new Format1Parser($src) ); } if ( $header == "format2" ) { return( new Format2Parser($src) ); } return(false); } } $parser = ParserFactory::GetParser( file_get_contents("file.txt") ); $parser->Parse();