Learn PHP Tutorial

PHP OOP – Inheritance


The mechanism of deriving a new class from an old one class is called inheritance.

Parent Class – The class from which new one class is deriver is called parent class. Also know as base class or super class.

Child Class – The class which are derived from old one class is called child class. Also known as derived class, extended class or sub class.

An inherited class is defined by using the keyword “extend”.

Declaration of Child Class

class ChildClassName extends ParentClassName
{
  //member of child class
}

Let us understand with an example:

<?php
class Car {
  public $name;
  public $color;

  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }

  public function intro() {
    echo "The car is {$this->name} and the color is {$this->color}.";
  }
}

// SUV is inherited from Car
class Suv extends Car {
  public function message() {
    echo "The car has a good ground clearance.";
  }
}

$car = new Suv ("Tata Nexon", "red");
$car->message();
$car->intro();
?>

OUTPUT
The car has good ground clearnace.
The car is Tata Nexon and the color is red.

Here, the Suv class is derived or inherited from the base class “Car”. This means the Suv class can use all the properties and methods of class “Car”. Hence $name and $color properties as well as public methods “__construct()” and “intro()” of parent class will be used in the child class “Suv” automatically. The Suv class can have its own methods as well. Here Suv class can use “__Construct()”, “intro()” and “message()” methods.

Types of Inheritance

  • Single Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance

Single Inheritance

Single Inheritance is a concept in PHP in which a class can be inherited by a single base class (parent class) only.

Syntax:

class Car
{
  //member of parent class
}

class Suv extends Car
{
   //member of child class
}

Example :

<?php
class Father{
  public $a;
  public $b;

  public function __construct($a, $b) {
    $this->a= $a;
    $this->b= $b;
  }
}

// Son class is inherited from Father
class Son extends Father {
  public function display() {
    echo "Value of A : ". $this->a. "<br>";
    echo "Value of B : ". $this->b. "<br>";
  }
}

$obj = new Son(10, 20);
$obj ->display();
?>

OUTPUT
Value of A : 10
Value of B : 20

Multi-level Inheritance

PHP OOP does not allow multiple inheritance, it allows only multilevel inheritance. In multi-level inheritance, the child class inherits the feature of single parent class only. A subclass can not extend more than one super class.

But PHP OOPS allows hierarchical inheritance, in which a child can inherit the property of their parent and parent can get property of grand parent and so on. In this way a child class can get property of parent and grand parents also, as parent class is inheriting grand parent properties.

Syntax:

class A {
   //Body of grand parent class A
}

class B extends A {
   //Body of parent class B
}

class C extends B {
   //Body of child class C
}

PHP – Inheritance and the Protected Access Modifier

We know that protected properties can only be accessed within the class and classes derived from that class.

Let us understand by an example-

<?php
class Car{
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name= $name;
    $this->color = $color;
  }
  protected function about() {
    echo "The car is {$this->name} and the color is {$this->color}.";
  }
}

class Suv extends Car {
  public function message() {
    echo "This is a SUV car having all features of a car along with special designed chasis.";
  }
}

// Try to call all three methods from outside class
$car = new Suv("Toyota", "black");  // OK. __construct() is public
$car->message(); // OK. message() is public
$car->about(); // ERROR. about() is protected
?>

So, here we see that if we try to call a protected method from outside of the class. It throw an error. Only public methods works fine. But we can access protected method if we call the protected method from inside the derived class.

Let us understand with an example

<?php
class Car{
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function about() {
    echo "The car is {$this->name} and the color is {$this->color}.";
  }
}

class Suv extends Car{
  public function message() {
    echo "This is a SUV car having all features of a car along with special designed chasis.";
    // Call protected method from within derived class - OK
    $this -> about();
  }
}

$car = new Car("Toyota", "black"); // OK. __construct() is public
$car->message(); // OK. message() is public and it calls about() (which is protected) from within the derived class
?>

PHP – Overriding Inherited Methods

In PHP OOP, Overriding concepts is an ability to override a method by redefining the same method in derived class. The overriding method has the same method name, number and type of parameters.

Let us understand with an example –

<?php
class Car {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function about() {
    echo "The car is {$this->name} and the color is {$this->color}.";
  }
}

class Suv extends Car {
  public $price;
  public function __construct($name, $color, $price) {
    $this->name = $name;
    $this->color = $color;
    $this->price= $price;
  }
  public function about() {
    echo "The car is {$this->name}, the color is {$this->color}, and the speed is Rs-{$this->price}.";
  }
}

$car = new Car("Toyota", "black", 34,00,000);
$car->about();
?>

PHP – The final Keyword

The final keyword prevents the class inheritance or to prevent overriding a method by prefixing the definition with final.

Final keyword can be used to prevent class inheritance. Please have a look on example given below:

<?php
final class Car {
  // some code
}

// will result in error
class Suv extends Car{
  // some code
}
?>

Final keyword is used to prevent method overriding.

<?php
class Car {
  final public function about() {
    // some code
  }
}

class Suv extends Car {
  // will result in error
  public function about() {
    // some code
  }
}
?>