Last active
August 29, 2015 14:21
-
-
Save fcaravana/b8bffe670cfe20210b83 to your computer and use it in GitHub Desktop.
Understanding and Applying Polymorphism in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Understanding and Applying Polymorphism in PHP: | |
http://code.tutsplus.com/tutorials/understanding-and-applying-polymorphism-in-php--net-14362 | |
*/ | |
interface MyInterface { | |
// methods | |
} | |
class MyClass implements MyInterface { | |
// methods | |
} | |
interface MyInterface { | |
public function doThis(); | |
public function doThat(); | |
public function setName($name); | |
} | |
// VALID | |
class MyClass implements MyInterface { | |
protected $name; | |
public function doThis() { | |
// code that does this | |
} | |
public function doThat() { | |
// code that does that | |
} | |
public function setName($name) { | |
$this->name = $name; | |
} | |
} | |
// INVALID | |
class MyClass implements MyInterface { | |
// missing doThis()! | |
private function doThat() { | |
// this should be public! | |
} | |
public function setName() { | |
// missing the name argument! | |
} | |
} | |
abstract class MyAbstract { | |
// methods | |
} | |
class MyClass extends MyAbstract { | |
// class methods | |
} | |
abstract class MyAbstract { | |
public $name; | |
public function doThis() { | |
// do this | |
} | |
abstract public function doThat(); | |
abstract public function setName($name); | |
} | |
class poly_base_Article { | |
public $title; | |
public $author; | |
public $date; | |
public $category; | |
public function __construct($title, $author, $date, $category = 0) { | |
$this->title = $title; | |
$this->author = $author; | |
$this->date = $date; | |
$this->category = $category; | |
} | |
} | |
class poly_base_Article { | |
//... | |
public function write($type) { | |
$ret = ''; | |
switch($type) { | |
case 'XML': | |
$ret = '<article>'; | |
$ret .= '<title>' . $obj->title . '</title>'; | |
$ret .= '<author>' . $obj->author . '</author>'; | |
$ret .= '<date>' . $obj->date . '</date>'; | |
$ret .= '<category>' . $obj->category . '</category>'; | |
$ret .= '</article>'; | |
break; | |
case 'JSON': | |
$array = array('article' => $obj); | |
$ret = json_encode($array); | |
break; | |
} | |
return $ret; | |
} | |
} | |
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 = '<article>'; | |
$ret .= '<title>' . $obj->title . '</title>'; | |
$ret .= '<author>' . $obj->author . '</author>'; | |
$ret .= '<date>' . $obj->date . '</date>'; | |
$ret .= '<category>' . $obj->category . '</category>'; | |
$ret .= '</article>'; | |
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); | |
} | |
} | |
class poly_base_Article { | |
//... | |
public function write(poly_writer_Writer $writer) { | |
return $writer->write($this); | |
} | |
} | |
class poly_base_Factory { | |
public static function getWriter() { | |
// grab request variable | |
$format = $_REQUEST['format']; | |
// construct our class name and check its existence | |
$class = 'poly_writer_' . $format . 'Writer'; | |
if(class_exists($class)) { | |
// return a new Writer object | |
return new $class(); | |
} | |
// otherwise we fail | |
throw new Exception('Unsupported format'); | |
} | |
} | |
$article = new poly_base_Article('Polymorphism', 'Steve', time(), 0); | |
try { | |
$writer = poly_base_Factory::getWriter(); | |
} | |
catch (Exception $e) { | |
$writer = new poly_writer_XMLWriter(); | |
} | |
echo $article->write($writer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment