PHP Inheritance

PHP Inheritance - When one class shares code from another class, then this process is known as inheritance.

In other words, when a class is derived from another class then this process is known as inheritance. The derived class is known as the child class or subclass or derived class and the other class from which the child is derived is the parent class or super class.

Followings are the features of inheritance:

  • Inheritance provides flexibility to write code in the parent class and use it in both parent and child classes.
  • The child class inherits all the public and protected properties and methods from the parent class. Although, it has its own properties and methods.
  • The child class uses the extends keyword when it inherits the parent class.

General Syntax

      class  SubClass extends SuperClass {

  }
    
Try it now

Source Code

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

// Strawberry is inherited from Fruit
class Mango extends Fruit {
  public function displayMessage() {
    echo "I have ".$this->name." having its color ".$this->color;
  }
}
$mango = new Mango("mango", "yellow");
$mango->displayMessage();

?>
        
Try it now

Let us understand the above example.

We inherited the Mango class from the Fruit class

The child class displayMessage() method access the property $name & $color of parent class.

Using Protected Access Modifier

Please keep in mind that the protected access modifier is visible in the defined class as well as inside the inheritance class.

By declaring a property and method to protect, you can not access outside of the child and parent class. Let us understand it with the help of an example.

Source Code

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

// Strawberry is inherited from Fruit
class Mango extends Fruit {
  public function displayMessage() {
    echo "I have ".$this->name." having its color ".$this->color;
  }
}
$mango = new Mango("mango", "yellow");
$mango->displayMessage();
//invalid statement
echo $mango->name;
echo $mango->color;
?>
        
Try it now

PHP - Overriding Inherited Methods

Inherited methods can be overridden inside the child class by redefining the same method.

Let us see it with the help of an example.

Source Code

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

// Strawberry is inherited from Fruit
class Mango extends Fruit {
  public function __construct($name, $color, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->weight = $weight;
  }
  public function basicInfo() {
    echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
  }
  
  public function displayMessage() {
    echo "I have ".$this->name." having its color ".$this->color;
  }
}


$obj = new Mango("Strawberry", "red", 50);
$obj->basicInfo();
?>
        
Try it now

PHP - The final Keyword

The final keyword is used for :

  • To prevent class inheritance
  • To prevent method overriding

General Syntax

      final class Fruit {
  // some code
}

//Error
class mango extends Fruit {
  // some code
}
    
Try it now

Source Code

          <?php
class Fruit {
  final public function intro() {
    // some code
  }
}

class Mango extends Fruit {
  // will result in error
  public function intro() {
    // some code
  }
}
?>
        
Try it now

Web Tutorials

PHP Inheritance
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4