PHP

Inheritance in PHP

Inheritance in PHP is useful when we need to create classes with some common, while few contrasting behaviors. In such case, we create a parent class for shared characteristics and child classes for distinct ones.

We discussed inheritance in our “Learn OOP in PHP” post, we should extend our code from example given there.

Setting Up Classes

For better understanding of inheritance in PHP, consider a scenario we might face while developing an E-Commerce website, We may be having products of different categories. Products related to distinct categories may contain attributes which could be identical to a product of other category. For example, a “shirt” in “Clothes” category would have a “title” attribute and a “laptop” in “Electronics” category would also have “title” property.

In the code above we are defining a parent class “Product” and extending two child classes named “shirt” and “laptop”.

Overriding properties and methods of parent class

In general, A property or method of a parent class with “public” or “protected” access modifiers is accessible from child class. If we re-declare a property or method of a parent class in child class, it is considered as Overridden.

In above code, we are re-declaring the property “$title” in Shirt class. The constructor of the parent class is echoing the $title property. Upon executing this code, you can see that it prints “Shirt Title”, which is defined in child class. It concludes that if an object of child class accesses a property which is overridden in child class than the return value would be same as defined in that child class.

Also notice that we have not defined any constructor in child class “Shirt”. This is the reason why constructor of parent class “Product” is called whenever any object is created from “Shirt” class.

Just like properties, we may also override methods in child classes. This is useful if we want objects of child class behave differently in some cases. Following code demonstrates the use of method overriding in PHP:

The method “summarize()” has been defined in parent class as well as in child class, now whenever an object of child class calls “summarize()”, the method defined in child class will be executed.

Using abstract classes to implement inheritance in PHP

Abstract classes are different from normal classes. It is not allowed to create objects directly from abstract classes. Abstract classes are incomplete in nature and they are dependent on child classes to complete their functionality.

Abstract classes exist to behave as a parent. Let’s observe their behavior by looking at the code below:

Executing above code results in a fatal error which says “Fatal error: Cannot instantiate abstract class Product”. We have to create a child class which should extend this abstract class “Product”, doing so, we may utilize the functionality of abstract class. Following code executes without any error:

Conclusion

In this post, we tried to experiment with the concept of inheritance in PHP. We also tried to keep our approach simple and learn this concept with as little code as possible.

About Zohaib Shah

Passionate software engineer with expertise in Django, Laravel and NodeJS. Working with different SaaS based products and API connected apps. Software Architect of UK's leading online travel agency. Get in touch
View all posts by Zohaib Shah →

1 thought on “Inheritance in PHP

Comments are closed.