Developers/Object oriented programming in PHP/Abstract classes and interfaces/en

Abstract classes
Abstract classes can be created with the keyword abstract. Besides implementing public/protected/private/static methods, they can also define abstract methods which only provide a blueprint of the methods that must be implemented.

Following rules apply to abstract classes:


 * abstract declared classes can't be instantiated and it's not possible to create objects of an abstract class.
 * Each non-abstract class extending an abstract class must implement the abstract methods and provide their full functionality.
 * The parameter signatures of the abstract and the implementing methods must be the same, but additional parameters are allowed.

There a three reasons to use abstract classes:


 * If there are methods that should be shared between multiple implementations.
 * If constants should be shared between different class implementations.
 * If the template design pattern should be used.

Each item and manager class in Arcavias extends an abstract class, e.g. the MShop_Common_Item_Abstract</tt> and MShop_Common_Manager_Abstract</tt> classes are extended by all items and managers:

Interfaces
Interfaces specify which public methods a class must implement. Therefore, they provide only the method names and the parameter signatures omitting the method contents. Methods in interfaces are defined like abstract</tt> methods in abstract</tt> classes and interfaces guarantee that all implementing methods have the same parameter signature and can be called in the same way.

Following rules apply to interfaces:


 * A class can implement multiple interfaces.
 * An interface can extend multiple interfaces.
 * Each class implementing an interface has to implement all methods defined in the interface.
 * The parameter signatures of the methods in the interface and the implementing classes must be identically.
 * When type hinting for parameters is used in an interface method, then the equivalent parameter of the implemented class method must be exactly of that type.

Interfaces establish a high level of security because it's guaranteed that all objects implementing an interface have the same methods and parameters, especially when used in combination with type hinting as this reveals wrong class implementations and parameters very quickly. As a rule of thumb, create an interface for every class type and only use interface for type hinting. This allows you to exchange a class implementation by another one without touching the existing code.

In Arcavias, interfaces are used for almost every class and are often extended by other interfaces to cover domain specific methods:

Differences between interfaces and abstract classes
There are a few but critical differences between interfaces and abstract classes:


 * An interface can extend multiple interfaces whereas (abstract) classes can only extend one class.
 * All methods of an interface have to be public</tt> where abstract methods can be either public</tt> or protected</tt>, but not private</tt>.
 * When an interface is added as parameter type in a method of an interface, then all objects are accepted, which implement the given interface directly or indirectly.