Why Use Interfaces in Web Design
Why PHP Interfaces in Web Design
By Tech Operative
I was asked the other day, “What is the point of PHP interfaces?” Interfaces were added to PHP in version 5. Why is in important in web design? If you come from a procedural programming background, this feature of PHP may seem rather pointless, but in my opinion, it is one of the most important advancements in the PHP language to date.
So what is an interface exactly anyways? An interface is a contract between a class and the outside world. An interface will define the signatures of methods that a particular class expects in an object that it accepts as an argument of either its constructor or one of its methods. The interface describes a class’ method signatures, but not the method’s implementation details.
Let’s examine the following code.
Notice that the barRunner() method clearly expects to accept an object as it argument, and the object needs to have a run() method. This code is pretty easy to understand, but imagine if the method expected an object with many specific methods that it needs to run. You could imagine that it might become complicated to figure out what type of object to give to this method as an argument.
You could solve this by simply using type hinting. Say we have a class that we would like to give to the barRunner() method, which has the expect run() method in its definition.
You could then easily type hint the barRunner() method’s argument to only accept Bar objects.
While this works, it has now cemented a relationship between the Foo and the Bar class. Foo’s barRunner() method will never be able to accept any objects except for Bar and descendents of Bar. What if some day you would like to accept other objects into this method that are not descendants of Bar however? Let’s say for instance you have created another class called QuickBar that you would also like to be able to run through barRunner()? This is where interfaces come into play. Instead of type hinting the barRunner() method argument with Bar, we would instead type hint it with BarInterface, and have all of the classes that we want to be able to run through barRunner() implement a BarInterface.
Implementing an interface with a class forces you to create methods for each of the interface’s described method signatures. This way you know that every object that you pass into the barRunner() method will have all of the required methods expected by the Foo class. This is a simplistic example, so it is hard to see the real benefits, but in more complicated programming patterns, interfaces become essential in web design.
This particular example describes the COMMAND programming pattern. In fact all programming patterns make use of interfaces. The use of interfaces makes your classes more agile and makes it much easier to modify your application at a later time. This is because the relationships between classes are no longer cemented. Instead classes have relationships with interfaces, which can be implemented by any class you choose. This is a HUGE benefit in a large project, and also makes creating mock objects for unit testing a breeze.