From the course: PHP: Object-Oriented Programming

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Define a subclass

Define a subclass

- [Instructor] Now that we have an overview of how class inheritance works and why we want to use it, let's learn to actually implement it in PHP by defining a subclass. The way that you define a subclass in PHP is by first defining the parent class, just like you normally would and then you define the subclass in the same way, except that you add another keyword after the class name, which is extends and then the name of the parent class that you want to inherit it from. That's all it takes to make a sub class. Now, the parent must be declared first or else PHP would complain when it gets to that part where it says extends parent, it would say I don't know what parent class you're talking about. So we must always declare the parent class first and then we can easily make a subclass from it, by using the keyword extends. To use the example from the previous movie, you can see here I have a parent class called document and…

Contents