Friday , 17 November 2017
Home / Programming / PHP / Inheritance – PHP OOP Tutorial
Inheritance
Inheritance

Inheritance – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here. http://jaskokoyn.com/php-oop-tutorial-series/ 

Let’s talk about inheritance. We’ve talked about how to extend a class and covered encapsulation. Inheritance is when a class extends another class and inherits the properties and methods of that class. The main advantage to this is the ability to use a parent class and modify it for a special use without actually modifying the actual class itself. You can create new methods and properties that can be used alongside the parent’s methods and properties.

Inheritance goes 1 way. Meaning, any methods or properties created in the child class, can not be used in the parent class. If we had Class B extend Class A, then Class B has access to all methods and properties of Class A, but not vice versa. If we had an extra class called Class C that extends Class B, then Class C has access to all methods and properties to both Class A and Class B.

Difference between Inheritance and Encapsulation

Remember that these are 2 concepts to object oriented programming. Encapsulation is the concept of how properties and methods are accessed. Inheritance is the concept of a subclass containing all the properties and methods of the parent class it extends.

It’s important to understand these 2 concepts as they are used regularly in PHP OOP. Actually, these 2 concepts apply to all programming languages that use object oriented programming. Such as Java, C#, or C++.

We’ve already seen an example of this in our CanadianPet class. Our CanadianPet class inherits all the properties and methods of the Pet class. By doing this, we don’t have to copy and paste code. Instead, we can override certain properties or methods in our subclass. You can also reuse code by doing so.

Conclusion

Now we’re learning the true power of object oriented programming. Inheritance is an important concept to understand. If you don’t fully understand, then go here for a more thorough explanation.

http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

About Jasko Koyn

Check Also

Abstract Classes

Abstract Classes – PHP OOP Tutorial

Today, we’ll be discussing what abstract classes are and how we could use them. There …

One comment

  1. There’s an easy and understandable explanation. thank you 🙂

Leave a Reply

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

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link