Interfaces vs. Abstract classes in PHP

Many developers do not know the main difference between intefaces 1 and abstract classes 2, since they seem to provide almost the same capabilities.
Hopefully this will help you decide which to use in your application.

When to use Interfaces?
Interfaces allow you to specify which methods a class must provide, without having to know how they are handled.
Methods specified in an interface MUST be implemented in a class.

interface SomeInterface
  public function doSomething( $variable );

When to use Abstract classes?
An abstract class provides structure. It usually defines default implementations.
Methods specified in an abstract class can be ignored in a subclass.

abstract class SomeInterface
  abstract protected function doSomething( $variable )
    //Put some code here if you like

This table attempts to define the difference between Interfaces and Abstract classes.

.comparison { border-collapse: collapse; margin-bottom: 10px; }
.comparison th { background-color: #F0F0F0; }
.comparison td, .comparison th { padding: 4px; border: 1px solid #F0F0F0; }

Feature Interface Abstract class
multiple inheritance: a class can implement several interfaces. A class can only extend one abstract class.
default implementation: An interface can not provide any code at all. An abstract class can provide default code, and parts that can be overridden.
constants: Interface constants work exactly like class constants, however, they can not be overridden. Abstract classes can have their constants overridden by extending classes.
convenience: Interfaces can easily be added to a third party class. Third party classes might have to be rewritten to extend the abstract class.
is-a vs -able or can-do: Interfaces describe the abilities of a class, and not the identity of the class (It defines what the class can do). An abstract class defines the code identity of the descendants. (It defines what the class is). For example: If you defined a Dog as abstract, then Shepherds are descendants of Dogs.
visibility: All declared methods MUST be public. Methods can be public or protected. If the abstract class defines a method as protected, then it can be declared as protected or public in the sub-class.
homogeneity: If various implementations share the same method signatures, the an interface should be used. If subclasses are going to different (heterogeneous), then use interfaces. If various implementations share a common “kind” or behavior, the an abstract class should be used. For example, if sub-classes are going to be similar (homogeneous), use abstract classes.
performance: Interfaces are almost double as slow as abstract classes. Pretty fast.
terseness: Interfaces are typically not designed to share. Shared code can be put in abstract classes.
maintenance: If you add a new method to an interface, then you MUST update all implementations of the interface and add code to implement the new method. If a new method is added to a class, you can provide default implementation for it. All existing code will continue to work without change.

Updated: Dec-05-2011: Added visibility to the table.




One thought on “Interfaces vs. Abstract classes in PHP

Leave a Reply