, , . -, poly_base_Article , , , -, .
$_GET.
class poly_base_Article {
private $title;
private $author;
private $date;
private $category;
public function __construct($title, $author, $date, $category = 0) {
$this->title = $title;
$this->author = $author;
$this->date = $date;
$this->category = $category;
}
public function getTitle() { return $this->title;}
public function AsArray()
{
return (array) $this;
}
public function write(poly_writer_Writer $writer) {
return $writer->write($this);
}
}
, , , .
* call_tool * factory poly_writer_Writer (, - ), -
class WriterFactory
{
public static function GetWriter($type='json')
{
switch($type)
{
case 'json'
case 'xml': $class= 'poly_writer_' . $type . 'Writer';
return new $class;
break;
default: throw new Exception("unsupported format: " . $type);
}
}
}
$article = new poly_base_Article('nPolymorphism', 'Steve', time(), 0);
$writer=WriterFactory::GetWriter(, $_GET['format']);
echo $writer->write($article);
, , , ( afa).
. , , , : P
, poly_base_Article, . , , ( ), , ( ).
WriterFactory factory, - , . , . , $. $ , , , . , WriterFactory, true/false , .
, fad... , . , - "" . : , - .
, , -, , , , , , 2 . SRP ( ).
, factory , , , , PHP . , , ( ). , .
, #, , , , (# , , PHP, , , , )