I, along with others, have written on interfaces many times before but recently I had occasion to find a new thing about them that makes them really awesome. A few days ago I was tasked with implementing PHPUnit against a Zend Framework application. This application, like many others, makes use of Zend_Auth, and in doing so makes use of the Zend_Auth_Storage_Session class. The problem with unit testing is that sessions aren’t supported very well, and I ran into all kinds of challenges when I tried to use the existing functionality.
But it turns out that Zend Framework includes an interface, called Zend_Auth_Storage_Interface, which defines the methods that must exist in a storage object. The Zend_Auth_Storage_Session class implements this interface (as it’s required to do by the type hinting in the Zend_Auth::setStorage() method). This meant that I could mock a storage object, give my mocked object to the Zend_Auth::setStorage() method, and avoid all the issues surrounding sessions and unit testing. And what’s more, because the interface defined for me the methods that needed to be implemented, I could have confidence that my tests functioned properly.
The unfortunate thing is that more PHP developers don’t implement interfaces when they could or should. They prefer to implement abstract or concrete classes and extend from there; however, interfaces offer three distinct advantages over inheritance in this case:
- More than one interface can be implemented, and the resulting object will identify with all the interfaces it implements. By contrast, when extending a class, only one class can be extended by another.
- Interfaces make mocking objects easy, because there’s no need to override various concrete methods and the interface contains no code. It’s simply the blueprint for what the object will look like.
- Since an interface already defines the majority of the public API, developers can feel comfortable that the methods they need will be implemented in each mock object they’re given, so long as it implements that interface. This provides fidelity in the tests. There’s nothing worse than discovering a test fails because of what was believed to be a “known” quantity.
Zend Framework implements a number of interfaces which makes developing against it much easier. Since interfaces can be implemented and the objects that implement them are considered objects of that type, users of interfaces find that the applications they’re working with can be that much more flexible.
Frustrated with your company’s development practices?
You don't have to be!
No matter what the issues are, they can be fixed. You can begin to shed light on these issues with my handy checklist.
Plus, I'll help you with strategies to approach the issues at the organization level and "punch above your weight."