For sometime, I have been leaning myself to the world of design patterns starting with the classical book of GoF. The book has examples written in C++. Though I have not done any project in C++, I am comfortable with its syntax. As I my choice of programming language has been C# and PHP, I am obviously trying/practicing to implement GoF concepts in these languages. Since C# has been highly inspired from C++, there has been no problem implementing these patterns in C#. And design patterns are language independent so, you can apply them in language of your choice.
But with PHP things are not so smooth. As we know that PHP is a dynamic language so, everything it interprets happen in runtime. This is what you expect from a scripting language. And with PHP 5, you have tons of object oriented features and syntax like interface, access modifiers, late binding, constant, type hinting and so on.
‘Programming to interface, not implementation’ is the underlying principle of the GoF Design patterns and may apply for all pattern based designs and architectures.
When you are involved in pattern based programming, you make heavy use of interface, composition and inheritance. Some experts prefer composition over inheritance (though this is still a debatable subject). Here is the simple concept of interface:
If A is the interface of B, then B is also a type of A.
Interface in PHP
interface ICar
{
function getSpeed();
function setSpeed($speed);
function carIsHybrid();
}
As interface can not be instantiate directly, it needs another object that implements interface to instantiate an interface.
Implementing interface
class Volt implements ICar
{
private $_speed;
private $_carIsHybrid = true;
public function getSpeed() {
return $this->_speed;
}
public function setSpeed($speed) {
$this->_speed = $speed;
}
public function carIsHybrid() {
return $this->_carIsHybrid;
}
public function __construct($speed) {
$this->setSpeed($speed);
}
}
Normally, one would instantiate Volt class and there would be no problem. But I am talking about interface based programming so, how do you instantiate an interface in PHP. In JAVA and C#, you would normally do like:
ICar car = new Car(200);
But this is not possible in PHP because variables in PHP does not any type (as with other dynamic languages). You can only instantiate class as:
$car = new Volt(200);
$car2 = new ICar();//not possible; will invoke error
One solution might be to check if the object is a type of interface using instanceof keyword.
$car = new Volt(200);
if ($car instanceof ICar) {
//do something
}
or one can wrap the whole stuff in a function like:
function cast(ICar $car) {
return $car;
}
…or even better way would be….
class MyCarObject {
static public function cast(ICar $object) {
return $object;
}
}
This is possible because PHP 5 supports type hinting.
Implementing the above interface wrapper:
$car = MyCarObject::cast(new Volt(200));
echo $car->getSpeed()."<br/>";
echo $car->carIsHybrid() ? "Car is hybrid" : "Same old gas guzzler";
Seems a bit waste of time but if you use interface based programming, you will understand what I am saying.
I would not have to do this all if PHP supports class type casting. Currently, PHP supports only pre-defined type casting of integer, string, boolean and so on. See here.