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:
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:
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:
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):
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: