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.
extends
keyword when it inherits the parent class.
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();
?>
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.
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;
?>
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();
?>
The final keyword is used for :
General Syntax
final class Fruit {
// some code
}
//Error
class mango extends Fruit {
// some code
}
Source Code
<?php
class Fruit {
final public function intro() {
// some code
}
}
class Mango extends Fruit {
// will result in error
public function intro() {
// some code
}
}
?>