PHP OOP – Inheritance

When a class descends from another class in Object-Oriented Programming, this is referred to as inheritance.

The derived class is known as child/subclass.

The class from which the child class is derived is known as the super/parent class.

The parent class’s public and protected properties and methods are passed down to the child class. A child can also have its own set of attributes and methods.

To establish a relationship between two classes, PHP uses the extends keyword.

Sntyax:

<?php
class A{
   //properties, constants and methods of class A
}
class B extends A{
   //public and protected methods inherited
   //can also have own methods and properties
}
?>

Any inherited method can be redefined or overridden by a child class.

Note: A child class can also become parent class of another class.

Let’s see an example below:

<?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}."; 
  }
}

// Revo is inherited from Fruit
class Revo extends Car {
  public function MSG() {
    echo "I am much expensive "; 
  }
}
$revo = new Revo("Revo", "black");
$revo->intro();
$revo->MSG();
?>

As in the above example because of inheritance, the Revo class can access the Car class’s public $name and $color attributes, as well as the public __construct() and intro() methods.

PHP – Overriding Inherited Methods

Redefining the methods in the child class (with the same name) can be used to override inherited methods.

<?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}."; 
  }
}

class Revo extends Car {
  public $quantity;
  public function __construct($name, $color, $quantity) {
    $this->name = $name;
    $this->color = $color;
    $this->quantity = $quantity; 
  }
  public function intro() {
    echo "The Car is {$this->name}, the color is {$this->color}, and the quantity is {$this->quantity}."; 
  }
}

$Revo = new Revo("Revo", "black", 2);
$Revo->intro();
?>

In the above example, The Child class Revo has overridden the methods(construct and intro) of the parent class.

Note: The final keyword can be used to prohibit method overriding or class inheritance.

Inheritance and Protected method

If you have a protected method in the parent class then you can use that method inside the child class, not by child class object.

Let’s see an example below to get it clearly.

We’ll see two protected methods of a parent class in the example below. We’ll compare the results of calling one method inside the child class and one outside the child class by using a child class object.

<!DOCTYPE html>
<html>
<body>

<?php
class Car {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color; 
  }
  protected function car_name() {
    echo "The Car is {$this->name}."; 
  }
  
   protected function car_color() {
    echo "The color of Car is {$this->color}."; 
  }
  
}

class Revo extends Car {
  public function MSG() {
    echo "I am much expensive.";
    // Call protected function from within Child class - OK 
    $this -> car_name();
  }
}
$revo = new Revo("Revo", "black");  // OK. __construct() is public
$revo->MSG(); // OK. MSG() is public 
$revo->car_color();
?>
 
</body>
</html>

As a result, because the car_color function was protected and invoked outside of the child class, an error occurred.