Mastering PHP Inheritance

Throughout this article, we will talk about PHP inheritance, including how to create parent and child classes, how inheritance can be applied to reuse code, and how to override methods in parent classes from child classes when necessary.

Object-oriented programming is a discipline in which you can create new types of classes based on the existing ones using a concept known as inheritance.

With inheritance, you can reuse code, create new classes, and add new functionality.

By the end of this article, you will understand PHP inheritance and create more modular, extensible, and efficient classes with it.



PHP Inheritance – What is it?

PHP inheritance is a mechanism that allows a class to inherit the properties and behaviors of another class. This can help reduce code duplication and make it easier to maintain and extend code over time.

There are a number of properties and methods that inherit from the parent class that are public and protected.

The class can also have its own methods and properties that can be used by the class.

Using the extend keyword, a class can be defined to inherit from another class.

Here is a simple example of creating parent and child class with printing data from child class with respect to parent class:

Example: 

<?php class Student { public $name; public $age; public $rollnum; public function __construct($name, $age, $rollnum) { $this->name = $name; $this->age = $age; $this->rollnum = $rollnum; } public function intro() { echo "Name: {$this->name} <br> Age: {$this->age}<br> Roll Number: {$this->rollnum}<br>"; } } // StudentResultInfo is inherited from Student class StudentResultInfo extends Student { public $totalmarks; public $percentage; public $grade;public function __construct($name, $age, $rollnum, $totalmarks, $percentage, $grade){ $this->name = $name; $this->age = $age; $this->rollnum = $rollnum; $this->totalmarks = $totalmarks; $this->percentage = $percentage; $this->grade = $grade;}public function marksDisplay() { echo "Result: <br> Total Marks: {$this->totalmarks}<br> Percentage: {$this->percentage}<br> Grade: {$this->grade}"; } } $std1 = new StudentResultInfo("John", 18, 1012, 450, 90, "A"); $std1->intro(); $std1->marksDisplay(); ?>

In above PHP inheritance example, The Student class is defined with three properties (name, age, and rollnum) and a method intro() that displays the values of those properties.

The StudentResultInfo class extends the Student class, inheriting its properties and methods. It also has three additional properties (totalmarks, percentage, and grade) and a method (marksDisplay()) that displays the values of those properties along with the student’s name, age, and roll number.

The constructor of StudentResultInfo calls the constructor of Student using the parent::__construct() method to initialize the inherited properties (name, age, and rollnum) and then initializes the new properties (totalmarks, percentage, and grade) with the values passed in.

Finally, an object of the StudentResultInfo class is created using the new keyword with arguments passed in for all the properties. The intro() and marksDisplay() methods are called on this object to display the student’s information and result.


PHP Inheritance & Protected Access Modifier

We learned in the previous chapter that you can access protected properties and methods within a class as well as by derived classes that derive from that class. What does that mean?

Here’s an example. We made a small change to the parent class method in order to see what output we get:

Example: 

<?php class Student { public $name; public $age; public $rollnum; public function __construct($name, $age, $rollnum) { $this->name = $name; $this->age = $age; $this->rollnum = $rollnum; } protected function intro() { echo "Name: {$this->name} <br> Age: {$this->age}<br> Roll Number: {$this->rollnum}<br>"; } } // StudentResultInfo is inherited from Student class StudentResultInfo extends Student { public $totalmarks; public $percentage; public $grade;public function __construct($name, $age, $rollnum, $totalmarks, $percentage, $grade){ $this->name = $name; $this->age = $age; $this->rollnum = $rollnum; $this->totalmarks = $totalmarks; $this->percentage = $percentage; $this->grade = $grade;}public function marksDisplay() { echo "Result: <br> Total Marks: {$this->totalmarks}<br> Percentage: {$this->percentage}<br> Grade: {$this->grade}"; } } $std1 = new StudentResultInfo("John", 18, 1012, 450, 90, "A"); $std1->intro(); $std1->marksDisplay(); ?>

The example above shows that an error will occur if we attempt to invoke a protected method intro() from outside the scope of the class.

We will get a warning. It will be fine to use public methods!

In order to illustrate this, let’s take another example:

Example: 

<?php class Student { public $name; public $age; public $rollnum; public function __construct($name, $age, $rollnum) { $this->name = $name; $this->age = $age; $this->rollnum = $rollnum; } protected function intro() { echo "Name: {$this->name} <br> Age: {$this->age}<br> Roll Number: {$this->rollnum}<br>"; } } // StudentResultInfo is inherited from Student class StudentResultInfo extends Student { public $totalmarks; public $percentage; public $grade;public function __construct($name, $age, $rollnum, $totalmarks, $percentage, $grade){ $this->name = $name; $this->age = $age; $this->rollnum = $rollnum; $this->totalmarks = $totalmarks; $this->percentage = $percentage; $this->grade = $grade;}public function marksDisplay() { $this->intro(); echo "Result: <br> Total Marks: {$this->totalmarks}<br> Percentage: {$this->percentage}<br> Grade: {$this->grade}";} } $std1 = new StudentResultInfo("John", 18, 1012, 450, 90, "A"); $std1->marksDisplay(); ?>

Taking a look at the example above, it is clear that everything works as expected. The reason for this is that the protected method intro() is called from within the class of the derived type.


Override Inherited Methods

PHP inheritance of methods can also be overridden by redefining the methods in the child class using the same name as the inherited methods.

You can see an example of this below. There will be a change in the construct() and intro() methods in the child class (StudentResultInfo) overriding the construct() and intro() methods from the parent class (Student):

Example: 

<?php class Student { public $name; public $age; public $rollnum; public function __construct($name, $age, $rollnum) { $this->name = $name; $this->age = $age; $this->rollnum = $rollnum; } protected function intro() { echo "Name: {$this->name} <br> Age: {$this->age}<br> Roll Number: {$this->rollnum}<br>"; } } // StudentResultInfo is inherited from Student class StudentResultInfo extends Student { public $totalmarks; public $percentage; public $grade;public function __construct($name, $age, $rollnum, $totalmarks, $percentage, $grade){ $this->name = $name; $this->age = $age; $this->rollnum = $rollnum; $this->totalmarks = $totalmarks; $this->percentage = $percentage; $this->grade = $grade;}public function intro() { echo "Name: {$this->name} <br> Age: {$this->age}<br> Roll Number: {$this->rollnum}<br> Result: <br> Total Marks: {$this->totalmarks}<br> Percentage: {$this->percentage}<br> Grade: {$this->grade}"; } } $std1 = new StudentResultInfo("John", 18, 1012, 450, 90, "A"); $std1->intro(); ?>

Example Explanation

This code is an example of method overriding in PHP. The Student class is defined with three properties (name, age, and rollnum) and a protected method intro() that displays the values of those properties.

The StudentResultInfo class extends the Student class, inheriting its properties and methods. It overrides the intro() method of the parent class by redefining it with a new implementation that displays the student’s information and result.

The constructor of StudentResultInfo is also defined with arguments for all the properties, but instead of calling the constructor of the parent class, it sets the properties directly using the $this keyword.

Finally, an object of the StudentResultInfo class is created using the new keyword with arguments passed in for all the properties.

The intro() method is called on this object, which displays the student’s information and result using the implementation defined in the StudentResultInfo class, not the parent class.

This demonstrates how child classes can override and provide their own implementation for methods inherited from parent classes, providing more specialized functionality.


PHP Final Keyword

You can use the final keyword as a way of preventing class inheritance from happening or preventing methods from being overridden.

This example illustrates how you can prevent the inheritance of classes in your code:

Example: 

<?php final class Student { // some code } //will result in error class StudentResultInfo extends Student { // some code } ?>
Here is an example of how to prevent a method from being overridden by another:

Example: 

<?php class Student { final public function intro() { // some code }} class StudentResultInfo extends Student { //will result in error public function intro() { // some code } } ?>
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *