Zend PHP 7 Certification – OOP – Inheritance

This post covers the Inheritance section of the OOP chapter when studying for the Zend PHP 7 Certification.

When you extend a class in most programming languages, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.

In PHP, the extends keyword is used in the class declaration to have the class inherit the properties and methods from another class.

<?php
class Foo
{
    public function printItem($string)
    {
        echo 'Foo: ' . $string . PHP_EOL;
    }
    
    public function printPHP()
    {
        echo 'PHP is great.' . PHP_EOL;
    }
}

class Bar extends Foo
{
    public function printItem($string)
    {
        echo 'Bar: ' . $string . PHP_EOL;
    }
}

$foo = new Foo();
$bar = new Bar();
$foo->printItem('baz'); // Output: 'Foo: baz'
$foo->printPHP();       // Output: 'PHP is great' 
$bar->printItem('baz'); // Output: 'Bar: baz'
$bar->printPHP();       // Output: 'PHP is great'

PHP does support Multi-level inheritance. It does not support multiple inheritance.

This means that you cannot have one class extend 2 other classes within the same declararion. However, you can have one class extend another, which extends another, and so on.

// Won't work
class A extends B extends C {

}

// Works
class A {
        // more code here
}

class B extends A {
        // more code here
}

class C extends B {
        // more code here
}

Although multiple inheritance is not supported, in PHP 5.4, traits were introduced which provide a mechanism for code reuse in single inheritance languages such as PHP.

It’s also worth noting that class or methods that are declared as final i.e. final class SomeClass, or final public function someMethod(), cannot be overridden.

Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract.

Methods defined as abstract simply declare the method’s signature – they cannot define the implementation.

Abstract classes can be thought of as ‘skeleton’ classes. An example of when to use abstract classes can be seen below.

<?php 
class Fruit { 
    private $color; 
    
    public function eat() 
    { 
        //chew 
    } 
    
    public function setColor($c) 
    { 
        $this->color = $c; 
    } 
} 

class Apple extends Fruit { 
    public function eat() 
    { 
        //chew until core 
    } 
} 

class Orange extends Fruit { 
    public function eat() 
    { 
        //peel 
        //chew 
    } 
}

Now I give you an apple and you eat it.

<?php 
$apple = new Apple(); 
$apple->eat();

What does it taste like? It tastes like an apple. Now I give you a fruit.

<?php 
$fruit = new Fruit(); 
$fruit->eat();

What does that taste like? Well, it doesn’t make much sense, so you shouldn’t be able to do that. This is accomplished by making the Fruit class abstract as well as the eat() method inside of it.

<?php 
abstract class Fruit { 
    private $color; 
    
    abstract public function eat(); 
    
    public function setColor($c) 
    { 
        $this->color = $c; 
    } 
}

When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility.

<?php
abstract class MyAbstract {
    abstract protected function doSomething();
    abstract protected function doSomethingElse();
}

class SomeClass extends MyAbstract {
    public function doSomething()
    {
        echo "Something public"; // Works
    }

    private function doSomethingElse()
    {
        echo "Something private"; // Won't work because of the stricter visibility
    }
}

View the other sections:

Note: This article is based on PHP version 7.0