PHP Inheritance
In object oriented programming, Inheritance enables a class to use properties and methods of an existing class. Often while coding we come across situations where in we have to create a new class with all the functionalities of an existing class and some additional methods, more like an extension to an existing class, in such cases, we can either copy all the properties and methods of the existing class into the new class to make them available in the new class or we can simply inherit the old class in the new class.
Confused? Let's take a simple example to understand it. Consider a class Human
with basic methods in it like walk()
, eat()
, hear()
, see()
etc. Now if we have to create two more classes for male and female with name Male
and Female
, with all the properties and methods of the class Human
and some specific features available only for Male
and Female
, we can do so by inheriting Human
class in Male
and Female
class.
The class which is inherited is called Parent class(or super class or base class) while the class which is inheriting other class is called as Child class(or sub class or derived class).
In the example above, 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 very useful if we want to create several similar classes. We can put the common properties and methods in one parent class and then inherit it in the child classes.
And for destroying the object Destructor method is used.
Syntax for Inheriting a Class
In PHP, extends
keyword is used to specify the name of the parent class 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
}
?>
Some important points to remember while using inheritance are:
- Child class can access and use only the non-private properties and methods on the parent class.
- Child class can have it's own methods too, which will not be available to the parent class.
- Child class can also override a method defined in the parent class and provide its own implementation for it.
Let's add a few methods to our Human
class and see how we can use them 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();
?>
Eve is walking...
Adam is seeing...
As you can see from the code above, both the child classes were empty, we only inherited the class Human
in both of them, which allowed them to access and use the member properties and methods on the parent class.
Child Class with its Own Methods and Properties
When a child class inherits a parent class, it can access and use all the non-private members of the parent class. We know that, but can a child class has its own member properties and methods? Yes, it can have. Let's take another example to see how we can do this:
<?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();
?>
Mercedes benz - Engine start...
I am Mercedes benz
Lets go on a drive...
When a class inherits another class, it gets an advantage of being able to use the properties and methods defined in the parent class without again defining them. And can also have its own properties and methods just like any other normal class.
The protected
Access Modifier
We learned about the various access modifiers and how we can use them to control access for various properties and methods of a class.
When a child class inherits a parent class, it can only access and re-use the non-private properties and methods. But we should not use public
access modifiers for the properties, as then the properties can be accessed from outside the class too.
To allow only the child class to access properties and methods of the parent class, we can use the protected
access modifier.
When we define any property or method of a class as protected
then those properties and methods can only be accessed within the child class which inherits the class.
Let's take an 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();
?>
Mercedes benz - Engine start...
I am Mercedes benz
Lets go on a drive...
In the example above we have made the name
variable as protected
, try running the same code with name as private
, you will get the following error:
Notice: Undefined Property...
Overriding Parent class Method
What if a child class wants to use a parent class method but slightly differently? It can do so by overriding the definition of the method defined in the parent class and provide its own definition. This is known as Method Overriding.
Let's take an example to understand this concept:
<?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();
?>
Car class drive method...
Motorcycle class drive method...
In the code above, we have a parent class named Vehicle
and two child classes extending the parent class namely Car
and Motorcycle
. In the parent class we have a method drive()
which we have overriden in the child classes and have provided different definition to it.
What if you do not want any child class to override the parent class method?
If we want so, we can define the method in the parent class as final
.
Let's see what will happen if we will 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();
?>
Fatal error: Cannot override final method Vehicle::drive()