PHP Interfaces: A class gotta do what it’s gotta do

These days I feel like in the 15 minutes that I spend watching TV, only educational good shows are on the channel with house fixing shows. I am not, in any way shape or form a handy man so it is nice to at least learn how things are supposed to get fixed. I am also super excited when I come across Planet Earth on BBC America but I can never catch it from the beginning. Even on Discovery channel, it is almost always some dudes fixing cars. At least on house fixing shows I can learn what to look for if I ever shop for a house. There is this handy guy generally named “the contractor” and he looks around the beat up house and writes down what the house has to have to function properly and that is the contract.

In programming world, I am almost certain you have heard house is an object and blue print of the house is the class. Now we can add one more thing. That contract the contractor hands out is the interface. A house has to have a kitchen, a bathroom, lights, and a bedroom. When you implement the house class to your own object, you expect those things to exist. In order to appeal to your nerdiness, lets try to give an example related to computers. A computer has to have a CPU right? If I implement a CPU class, is it wrong of me to expect a getCpu() method that returns information about the CPU? Of course not. How can I make sure this method exists in any other class that implements the CPU class? I write an interface for it.


interface Computer
{
public function getCPU();
public function getRam();
public function getHardDrive();
}

Now If I want to write my own class that implements this interface, I have to have these methods in it. Remember that contractor guy I mentioned above? Think about how much of a trouble he would be in if he does not obey by what is written. You will be in a lot of trouble if you do not obey the interface (the contract) as well.


class BaseServer implements Computer
{
protected $cpu;
protected $ram;
protected $hdd;

public function __construct($name)
{
$this->cpu = $cpu;
$this->ram = $ram;
$this->hdd = $hdd;
}

public function getCPU()
{
return $this->cpu;
}

public function getRam()
{
return $this->ram;
}

public function getHardDrive()
{
return $this->hdd;
}
}

If your class implements in interface but it does not contain every method defined within the interface, code will fail. You may think, what is point of this? Take a look at the not so creative example again. It is of course normal that a computer class has a function to get information about the CPU. Lets say that this class does not have an interface and two other classes implements this class. We are humans and we can be forgetful. We may forget about implementing an expected method and couple of coding hours later your code fails. Take a look at another possibility. Two other classes implement this class and they have all the methods to get information about CPU, ram and hard drive. What are those methods called? If I implement one of them and I want to get hard drive information, what do I call getHDD() or getHardDriveInfo() or getSSD ? You see where I am going with this? Inconsistency is almost a guarantee if you do not implement an interface. For the sake of peace of mind, use an interface so you know what you are getting yourself into.

In the world of Java and / or C variants, you might have heard the term polymorphism. This is kind of that. You are making the class responsible for it’s code and data but they have to have these methods.

Posted in PHP