Home » PHP » PHP Inheritance
This tutorial helps you learn about PHP inheritance that lets you reuse the code from another class. So, let us begin!
PHP Inheritance enables a class to use properties and methods of another class without duplicating it. Often, we are required to create a new class with all the functionalities of an existing class and some additional functionalities; basically, this new class is more like an extension of an existing class. In such cases, you have two options. First, copy all the properties and methods of the existing class into the new class and use them in the new class; secondly, you can inherit the old class in the new class.
Let us understand this using an example. First, assume a class Human with basic methods and properties in it such as eat(), walk(), see(), hear(), etc. Now, suppose you want to create two more classes for male and female with the name Male and Female, respectively, along with all the properties and methods of the class Human and some additional features available only for Male and Female. In that case, we can do so by inheriting the Human class in the Male and Female classes.
The inherited class is referred to as the Parent class(or super class or base class), while the class inheriting another class is called the Child class(or sub class or derived class).
In the above-mentioned example, Human will be the parent class, and Male and Female will be its child classes.
A class can be inherited by multiple classes.
Inheritance is beneficial if you need to create several similar classes. You can put the common properties and methods in one parent class and then inherit them in the child classes.
You can use the Destructor method for destroying the object.
In PHP, you can use extends keyword for specifying the parent class's name while defining the child class. For example,
<?php
class Human {
// parent class code
}
class Male extends Human {
// child class code
}
class Female extends Human {
// child class code
}
?>
Points to remember while using inheritance:
In the above-mentioned example, let's add a few methods to our Human class and see how these can be used in the child classes Male and Female.
<?php
// parent class
class Human {
// public property name
public $name;
// public function walk
public function walk() {
echo $this->name. " is walking...<br/>";
}
// public function see
public function see() {
echo $this->name. " is seeing...<br/>";
}
}
// child class
class Male extends Human {
// No code in child
}
// child class
class Female extends Human {
// No code in child
}
$male = new Male();
$male->name = "Adam";
$female = new Female();
$female->name = "Eve";
// calling Human class methods
// check the output (hehe, pun intended)
$female->walk();
$male->see();
?>
Output:
Eve is walking...
Adam is seeing...
As seen in the code above, both the child classes were empty; only class Human was inherited in both of them, which enabled the child classes to access and use the member properties on the parent class.
If a child class inherits a parent class, all the non-private members of the parent class can be accessed and used by the child class. But a child class can have its own member properties and methods. Let us see how we can do it through another example:
<?php
// parent class
class Vehicle {
// public property name
public $name;
// public function start
public function start() {
echo $this->name. " - Engine start...<br/>";
}
// public function stop
public function stop() {
echo $this->name. " - Engine stop...<br/>";
}
}
// child class
class Car extends Vehicle {
public function drive() {
echo "I am " . $this->name . "<br/>";
echo "Lets go on a drive...";
}
}
// child class
class Motorcycle extends Vehicle {
// motorcycle specific properties
// and methods
}
$car = new Car();
$car->name = "Mercedes benz";
// calling parent class method
$car->start();
// calling child class method
$car->drive();
?>
Output:
Mercedes benz - Engine start...
I am Mercedes benz
Lets go on a drive...
The advantage a class gets on inheriting another class is the ability to use the properties and methods of the parent class without defining them again. Moreover, the child class can also have properties and methods of its own, just like any other normal class.
We learned how to use the various access modifiers to control access for various properties and methods of a class.
When a parent class is inherited by a child class, it can only access and reuse the non-private properties and methods of a parent class. But the public access modifiers should not be used for the properties; because the properties can then be accessed from outside the class too.
The protected access modifiers can be used to allow only the child class to access the parent class's properties and methods.
When the property or method of a class has been defined as protected, then those properties and methods are only accessible within the child class which inherits the parent class.
Example:
<?php
// parent class
class Vehicle {
// protected property name
protected $name;
// public function start
public function start() {
echo $this->name. " - Engine start...<br/>";
}
// public function stop
public function stop() {
echo $this->name. " - Engine stop...<br/>";
}
}
// child class
class Car extends Vehicle {
public function drive() {
// accessing name variable of Car class
echo "I am " . $this->name . "<br/>";
echo "Lets go on a drive...";
}
}
$car = new Car();
$car->name = "Mercedes benz";
// calling parent class method
$car->start();
// calling child class method
$car->drive();
?>
Output:
Mercedes benz - Engine start...
I am Mercedes benz
Lets go on a drive...
In the above-mentioned example, we have made the name variable as protected. Now, try to run the same code with the name as private, and you will get the following error:
Notice: Undefined Property...
In Method Overriding, a child class can use a parent class method with slight differences by overriding the definition of the method that has been defined in the parent class and providing its own definition.
Let's understand this concept through an example:
<?php
// parent class
class Vehicle {
// public property name
public $name;
// public method
public function drive() {
echo "Vehicle class drive method...<br/>";
}
}
// child class
class Car extends Vehicle {
public function drive() {
echo "Car class drive method...<br/>";
}
}
// child class
class Motorcycle extends Vehicle {
public function drive() {
echo "Motorcycle class drive method...<br/>";
}
}
$car = new Car();
$car->name = "Mercedes benz";
// calling child class method
$car->drive();
$bike = new Motorcycle();
$bike->name = "Triumph Tiger";
// calling child class method
$bike->drive();
?>
Output:
Car class drive method...
Motorcycle class drive method...
There is a parent class named Vehicle in the above-mentioned code and two child classes extending the parent class named Car and Motorcycle. In addition, there is a method drive () in the parent class that we have overridden in the child classes and have defined a different definition to it.
In case you do not want any child class to override the parent class method, then you can define the method in the parent class as final.
Now, let us see what happens if we try to override a final method.
<?php
// parent class
class Vehicle {
// public property name
public $name;
// public method
final public function drive() {
echo "Vehicle class drive method...<br/>";
}
}
// child class
class Car extends Vehicle {
public function drive() {
echo "Car class drive method...<br/>";
}
}
$car = new Car();
$car->name = "Mercedes benz";
// calling child class method
$car->drive();
?>
Output:
Fatal error: Cannot override final method Vehicle::drive()