When a class derives from another class, it is said to be inheriting in OOP.
All of the parent class's public and protected properties and methods will be inherited by the child class. It may also have unique features and procedures.
The extends keyword is used to specify an inherited class.
Let's examine an illustration:
<?php
class Car
{
public $name;
public $color;
public function __construct($name, $color)
{
$this->name = $name;
$this->color = $color;
}
public function intro()
{
echo "This Car name is {$this->name} and the color is {$this->color}.";
}
}
#Bmw is inherited from Car
class Bmw extends Car
{
public function message()
{
echo "Hi, i am BMW.";
}
}
$bmw = new Bmw("BMW", "Black");
$bmw->message();
$bmw->intro();
#Output
"Hi, i am BMW. This Car name is BMW and the color is Black"
The Car class is inherited from the Bmw class. This means that the Car class can use the public $name and $color properties as well as the public __construct() and intro() methods from the Bmw class because of inheritance.
The Car class also has its own method: message().
Hope this article will help you to understand about INHERITENCE