HomePHP Page 2 - User-defined Interfaces in PHP 5: Introduction to Core Concepts
What are interfaces? Defining core concepts - PHP
PHP5 takes users increasingly in the direction of object-oriented programming. The Standard PHP Library (SPL) is a new item that helps developers both with the creation of OOP applications and the maintenance of standardization. The SPL enables developers to work with user-defined interfaces. In this article, the first of a series, Alejandro Gervasio uses examples to introduce you to working with these interfaces, which can keep you from having to reinvent the wheel with each new project.
Going back to the basics, in PHP4 an object is instantiated naturally from its corresponding class, and can be extended from a single parent class. Thus inheritance from multiple parent classes is not supported.
In simple terms, this means that once an object is created, it belongs only to the type defined within its original class. While multiple inheritance is still not available in PHP5, it supports the use of interfaces -- that is, programming structures that allow to define an object as belonging to different types -- by sharing multiple sets of methods.
Very often confused with the functionality provided by abstract classes, interfaces define a set of abstract methods and properties without an explicit definition that may be shared by objects that usually have nothing in common. As for abstract classes, interfaces establish a model of characteristics that will be expected to be exposed by an object when used across a program.
The syntax to implement interfaces within classes is very simple. To use them, simply add the “implements” keyword followed by the name of the interface, after the class name, as shown below:
class Example implements Iterator { // class definition code }
Or, in the case of a child class using an interface, you add the “implements” keyword after the “extends” keyword, like this:
class ChildClass extends ParentClass implements Iterator { // class definition code }
As I said previously, a class can implement multiple interfaces at the same time, as follows:
class Example implements Iterator, ArrayAccess { // class definition code }
As you can see, the syntax is quite simple, therefore implementing interfaces is fairly easy to do. Besides the required syntax, when a class implements an interface, it must provide an explicit implementation for each method declared within the interface itself (unless the class will be declared abstract). Otherwise it will result in a fatal error.
To many inexperienced users, the functionality of interfaces isn’t very clear. Since you already have the possibility to work with abstract classes and use their methods and properties in derived subclasses, the existence of an additional programming structure seems to be rather redundant. This is definitely a misconception, since interfaces are useful for specifying generically the functionality for objects of different types, by defining a set of methods (and properties) that will behave differently according to the object’s type.
Does this sound confusing? Don’t worry. At this point, with the brief theory behind PHP interfaces under our belts, I’ll write a couple of examples that implement user-defined interfaces, so their functionality can be properly understood.