Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Jan 17 2009

PHP and Interface – Some thoughts

Published by at 7:12 pm under PHP

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.


2 responses so far

2 Responses to “PHP and Interface – Some thoughts”

  1. Jani Hartikainenon 27 Jan 2009 at 2:38 am

    I don't think there's any reason why you would need typecasting. In a dynamic language you can just use duck typing anyway.

    If you want to actually make your code work with just the interface, you simply use just the methods provided by the interface, or risk using the other methods, which will possibly end in an error in case the actual implementation changes sometime. It's the same in Java or C# – you could attempt to cast your interface into a concrete class, but it might not work.

    Also, at least NetBeans will display auto-completion for only the methods of the interface, if the variable is a type-hinted method parameter, or a class member variable with a phpdoc @var declaration for the interface. Type hinting method variables would also allow you to enforce the interface for the parameters if you want, but I don't think there's any point to use it for faking typecasts.

  2. vibram fivefingerson 05 Jul 2010 at 3:02 am

    Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
    air jordan 16.5
    If its common it might be difficult to get, however, be creative and you can usually find something.

RSS Feed
Subscribe by email
Follow me @ Twitter