What is Inheritance in PHP?

In PHP, inheritance allows a class to inherit properties and methods from another class, promoting code reuse and creating a hierarchy of classes.
It means when a class derives from another class. The child class will inherit all the protected and
public properties and methods from the parent class. We can inherit a class by using the extends keyword.
it can have its properties and methods.
It is a way to access one class from another class.
It is a mechanism of extending an existing class by inheriting a class.

There are three main types of inheritance in PHP

  1. Single Inheritance.
  2. Multiple Inheritance (Not Supported)
  3. Multilevel Inheritance.
  4. Hierarchical Inheritance

 

1. Single Inheritance
   In single inheritance, a class can inherit from only one parent class.

 

Example 1
<?php
class Parent {
  public $name;
  public function __construct($name) {
      $this->name = $name;
  }  
  public function sound() {
      return "Some generic sound";
  }
}
class Dog extends Animal {
  public function sound() {
      return "Woof!";
  }
}
$dog = new Dog("Rex");
echo $dog->name . " says " . $dog->sound();
?>
Output
Rex says Woof!

Example 2
<?php
class Car {
public $name;
public $color;
public function __construct($name, $color) {
  $this->name = $name;
  $this->color = $color;
}
public function intro() {
  echo "We can travel with  {$this->name} and Its color is {$this->color}";
}
}
// Airplane is inherited from Car
class Airplane extends Car{
public function message() {
  echo "You can travel with Airplane- Boeing 747"."<br>";
}
}
$strawberry = new Airplane("Thar", "red");
$strawberry->message();
$strawberry->intro();
?>
Output
You can travel with Airplane- Boeing 747
We can travel with Thar and Its color is red

2. Multiple Inheritance. 
    In multiple inheritance isn't supported directly, but there are other ways to achieve  Multiple inheritance that is an Interface.
    we will learn later topic.
   Example:  Let's suppose there are three or more classes C,A, D and B class extends C,A, D class then we can not achieve.

3. Multilevel Inheritance
   In multilevel inheritance, a class can inherit from a parent class, and another class can inherit from this child class, forming a chain.

Example1
<?php
 class Animal {
  public function sound() {
      return "Some generic sound";
  }
}
class Dog extends Animal {
  public function sound() {
      return "Woof!";
  }
}
class GermanShepherd extends Dog {
  public function sound() {
      return "Woof Woof!";
  }
}
$gs = new GermanShepherd();
echo $gs->sound();
?>
 Output
Woof Woof!

4. Hierarchical Inheritance
Example 1
<?php
class Animal {
  public function sound() {
      return "Some generic sound";
  }
}
class Dog extends Animal {
  public function sound() {
      return "Woof!";
  }
}
class Cat extends Animal {
  public function sound() {
      return "Meow!";
  }
}
$dog = new Dog();
$cat = new Cat();
echo $dog->sound();
echo $cat->sound();
?>
Output
Woof!
Meow!


Prev Next