Polymorphism is one of the major Object Oriented Programming features (OOP) mature programming languages like C++ or JAVA implement. Though OOP experts may reasonably claim that PHP is not strictly Object Oriented (since nothing is an object in PHP unless you create it), the language has become polymorphism-ready with version PHP5.
What is polymorphism?
Its principle lies in its name : [poly-] means much, many and [-morphism] means forms, states. So, programming in a polymorphic way allows the developer to have one single object behaving differently according to its type. Practically speaking, the developer writes a base class which defines the basic features of the object and as many subclasses as necessary to build the different behaviours the object may have. Calling the same method will have the object’s children behave differently.
Here is a simple example of polymorphic design with a vehicle object. The Vehicle class has as attributes pieces of information that are found in every kind of vehicle : make, registering date, price :
Class Vehicle {
protected $make;protected $date;protected $price;public function __construct($make,$date,$price) {$this->make = $make;$this->date= $date;$this->price= $price;}}
Then create the plane class and the car class that will inherit the vehicle class :
Class Car Extends Vehicle {
private $engine;private $mileage;public function __construct($array) {parent::__construct($array["make"],$array["date"],$array["price"]);$this->engine = $array["engine"];$this->mileage = $array["mileage"];}}
Class Plane Extends Vehicule {
private $type;private $seats;public function __construct($array) {parent::__construct($array["make"],$array["date"],$array["price"]);$this->type = $array["type"];$this->seats = $array["seats"];}}
Structuring your classes that way allows you to make your OOP more abstract, thus more flexible. Changing a specific type of object after production time will be made easier and more stable.
Objects without polymorphic design
Imagine you want to create a form generator class without polymorphism :
<?phpclass Form {public function genInput($type,$name,$value) {SWITCH($type) {Case”text”:print “<input type=\”text\” name=\”".$name.”\” value=\”".$value.”\” />\r\n”;break;Case”textarea”:print “<textarea name=\”".$name.”\”>”.$value.”</textarea>\r\n”;break;}}}$form = new Form(); //initialize form object$form->genInput(“text”,”field1″,”Lorem ipsum…”); //prints <input type=”text” name=”field1″ value=”Lorem ipsum…” />$form->genInput(“textarea”,”field2″,”Lorem ipsum…”); //prints <textarea name=”field2″>Lorem ipsum…</textarea>?>
Objects with polymorphic design
//INTERFACE
interface Form {public function genInput();}
//ABSTRACT CLASSabstract class Input {protected $name;protected $value;public function __construct($name,$value) {$this->name = $name;$this->value = $value;}}
//TEXT & PASSWORD & HIDDEN INPUTclass Text extends Input implements Form {private $type;private $size;public function __construct($array) {parent::__construct($array["name"],$array["value"]);$this->type = $array["type"];$this->size = $array["size"];}public function genInput() {return “<input type=\”".$this->type.”\” name=\”".$this->name.”\” value=\”".$this->value.”\” size=\”".$this->size.”\” />\r\n”;}}//TEXTAREA INPUTclass Textarea extends Input implements Form {private $cols;private $rows;public function __construct($array) {parent::__construct($array["name"],$array["value"]);$this->cols = $array["cols"];$this->rows = $array["rows"];}public function genInput() {return “<textarea name=\”".$this->name.”\” cols=\”".$this->cols.”\” rows=\”".$this->rows.”\”>”.$this->value.”</textarea>\r\n”;}}
$object = new Text(array(“type”=>”text”,”name”=>”form_id”,”value”=>$item,”size”=>20));$hidden = $object->genInput();
$object = new Textarea(array(“name”=>”form_txta”,”value”=>$item ,”cols”=>20 ,”rows”=>5));$textarea = $object->genInput();
Abstract classes and interfaces
Devshed polymorphism in PHP5 article
PHP5 Tutorial : Abstract classes and Interfaces
PHP5 OOP abstract classes & interfaces