Understanding Abstract Classes in PHP

Abstract classes are an often misunderstood feature of PHP object-oriented programming (OOP) and the source of confusion when considered versus an Interface. The obvious reason for using an Interface is that a child class can implement multiple interfaces but extend only a single abstract class. However, if multiple inheritance is not required then people often go with abstract classes just because they provide the option of later adding base functionality within the abstract class. This is not entirely unreasonable but the reasons for creating abstract classes should be more than that.

Why Use Abstract Classes?

An Abstract class provides concrete base functions as well as abstract functions that must be implemented by concrete child classes—binding them into a contract so to speak, if they wish to make use of the base functionality.

This is a subtle but important point and this is where abstract classes really shine. They can call abstract functions from within base concrete functions. Jumping straight to an example is the clearest way to explain this.

abstract class Animal {
  function greeting() {
    $sound = $this->sound();      // exists in child class by contract
    return strtoupper($sound);
  }
  abstract function sound();      // this is the contract
}

class Dog extends Animal {
  function sound() {              // concrete implementation is mandatory
    return "Woof!";
  }
}

$dog = new Dog();
echo $dog->greeting();            // WOOF!

This opens up a whole lot of interesting possibilities. For example, you can write a drive() function that calls $this->start(); $this->accelerate(); in an abstract class. Then create a motorcycle class that defines its own start() and accelerate() functions that may be different from those in the car class. In turn, the motorcycle and car can both be driven by just calling drive() without having to implement it locally.

Characteristics of Abstract Classes

Make a note of these characteristics to lock down your understanding of abstract classes:

  1. Single inheritance. Child classes can extend only one class at a time.
  2. Abstract classes cannot be instantiated — no new Animal();
  3. Abstract classes can define class variables of type const only.
  4. Abstract class A can be extended by another abstract class B. Abstract class B can implement none or any of the abstract functions in A.
  5. In the previous case, a child class C which extends abstract class B must implement all abstract functions in B as well as the abstract functions in A which have not already been implemented in B.
  6. The signature of the concrete functions and abstract functions must be the same. However, if an abstract function is defined as abstract function speak($greeting); then it is okay to implement it as function speak($greeting, $shout = FALSE) but not function speak($greeting, $shout).
  7. The visibility of functions in the child classes must be the same or less restrictive than the parent class. Thus, a protected abstract function can be implemented as either protected or public but not private.
  8. Declaring functions as static abstract throws a strict warning in PHP 5.2 or earlier, however, as of PHP 5.3 this is allowed.

6 Responses



This article is no longer open for comments