可以使用不同的工具完成同樣的事情。 所以我在下面的例子。PHP OOP:接口與非接口方法 - 示例
一個顯示使用接口/多態(來源:nettuts - 我認爲)。另一個直接的類交互(我的) - 它也顯示了一些多態(通過call_tool())。
你能告訴我,你認爲哪種方式更好?
哪個更安全,更穩定,防篡改,未來的證明(關於afa代碼開發)。
請仔細檢查兩者中使用的範圍/可見性。
您的一般建議,這是最好的編碼實踐。
接口:
class poly_base_Article { public $title; public $author; public $date; public $category; public function __construct($title, $author, $date, $category = 0, $type = 'json') { $this->title = $title; $this->author = $author; $this->date = $date; $this->category = $category; $this->type = $type; } public function call_tool() { $class = 'poly_writer_' . $this->type . 'Writer'; if (class_exists($class)) { return new $class; } else { throw new Exception("unsupported format: " . $this->type); } } public function write(poly_writer_Writer $writer) { return $writer->write($this); } } interface poly_writer_Writer { public function write(poly_base_Article $obj); } class poly_writer_xmlWriter implements poly_writer_Writer { public function write(poly_base_Article $obj) { $ret = ''; $ret .= '' . $obj->title . ''; $ret .= '' . $obj->author . ''; $ret .= '' . $obj->date . ''; $ret .= '' . $obj->category . ''; $ret .= ''; return $ret; } } class poly_writer_jsonWriter implements poly_writer_Writer { public function write(poly_base_Article $obj) { $array = array('article' => $obj); return json_encode($array); } } $article = new poly_base_Article('Polymorphism', 'Steve', time(), 0, $_GET['format']); echo $article->write($article->call_tool());
非接口
class npoly_base_Article { public $title; public $author; public $date; public $category; public function __construct($title, $author, $date, $category = 0, $type = 'json') { $this->title = $title; $this->author = $author; $this->date = $date; $this->category = $category; $this->type = $type; //encoding type - default:json } public function call_tool() { //call tool function if exist $class = 'npoly_writer_' . $this->type . 'Writer'; if (class_exists($class)) { $cls = new $class; return $cls->write($this); } else { throw new Exception("unsupported format: " . $this->type); } } } class npoly_writer_jsonWriter { public function write(npoly_base_Article $obj) { $array = array('article' => $obj); return json_encode($array); } } class npoly_writer_xmlWriter { public function write(poly_base_Article $obj) { $ret = ''; $ret .= '' . $obj->title . ''; $ret .= '' . $obj->author . ''; $ret .= '' . $obj->date . ''; $ret .= '' . $obj->category . ''; $ret .= ''; return $ret; } } $article = new npoly_base_Article('nPolymorphism', 'Steve', time(), 0, $_GET['format']); echo$article->call_tool();
MikeSW代碼(如果我得到它的權利)
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 setTitle($title) { return $this->title = $title; } public function getTitle() { return $this->title; } public function getAuthor() { return $this->author; } public function getDate() { return $this->date; } public function getCategory() { return $this->category; } } interface poly_writer_Writer { public function write(poly_base_Article $obj); } class poly_writer_xmlWriter implements poly_writer_Writer { public function write(poly_base_Article $obj) { $ret = ''; $ret .= '' . $obj->getTitle() . ''; $ret .= '' . $obj->getAuthor() . ''; $ret .= '' . $obj->getDate() . ''; $ret .= '' . $obj->getCategory() . ''; $ret .= ''; return $ret; } } class poly_writer_jsonWriter implements poly_writer_Writer { public function write(poly_base_Article $obj) { //array replacement //$obj_array = array('title' => $obj->getTitle(), 'author' => $obj->getAuthor(), 'date' => $obj->getDate(), 'category' => $obj->getCategory()); //$array = array('article' => $obj_array); $array = array('article' => $obj); //$obj arrives empty return json_encode($array); } } 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);
$ _GET - 那真的不是q的一部分。 ;),所以我沒有太多注意。在實時代碼中,它將從各個角度進行適當的觀察。但是你是對的,我不應該讓它通過,即使這是沒有問題的。範圍。 – Jeffz 2012-03-07 21:18:47
如果您將poly_base_Article屬性設爲私有,如何poly_writer_xmlWriter訪問它們?他們之間沒有任何關係 – Jeffz 2012-03-07 21:31:17
WriterFactory - 是不是隻是創建附加的對象沒有太多的功能添加它呢? – Jeffz 2012-03-07 21:34:08