jump to navigation

Understanding Abstract Classes in PHP October 11, 2010

Posted by Tournas Dimitrios in PHP.

Welcome to another part of the serie ” The basics of OOP with PHP ” follow up …

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 protectedabstract 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.
  9. Abstract classes can have consts, members, method stubs and defined methods, whereas interfaces can only have consts and methods stubs.
  10. Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public.
  11. When inheriting an abstract class, the child class must define the abstract methods, whereas an interface can extend another interface and methods don’t have to be defined.
  12. A child class can only extend a single abstract (or any other) class, whereas an interface can extend or a class can implement multiple other interfaces.
  13. A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility.

Read more on PHP.net


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s