Join Kevin Skoglund for an in-depth discussion in this video Define a subclass, part of PHP: Object-Oriented Programming.
- [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 from. That's all it takes to make a subclass.
Now, the parent must be declared first, or else PHP would complain when it gets to the 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 it has a property and a method in it, and then I have three classes that are subclasses. ProductBrochure, WarrantyDocument, and CleaningInstructions.
All of those extend the document, so they use that keyword extends, and then the name of that parent class, Document. All three of those classes now have that same property and that same method in them. Here's another example using Vehicle as a parent class. It has a subclass of Car, and a subclass of Motorcycle. The fourth class that's being declared is CompactCar, and you'll notice that it doesn't extend Vehicle, it extends Car.
So it gets all the properties and methods in Vehicle, but also any additional or overridden behaviors in the Car class. You can visualize it like this. It's perfectly fine for subclasses to also have additional subclasses. All of the behaviors would just trickle down to the subclasses. PHP has several built-in functions which are useful when working with inheritance. Get_parent_class, which can either take the name of the class or an instance of the class as an argument.
Is_subclass_of, which also can take the name of a class or an instance as its first argument, and then the parent class. You want to find out if something is a subclass of whatever that parent class might be, or we can ask for all class parents. We can pass in either the name of the class or we can pass in an instance and it'll tell us all of the parent. So in the case of the example we just saw, CompactCar would have parents that included both Vehicle and Car. Let's try defining subclasses in our sandbox.
I've already created a PHP file called class_inheritance.php I'm going to drag in here. That's also included in your exercise files. I just wanted to get us started here with a basic class. So I've got class of User, which as $first_name, $last_name and $username. I've got a full_name method, which is familiar to us by now. It's just taking those different properties and concatenating them together. Then notice I've got a subclass here called Customer. Customer extends User. So now Customer is a subclass of User.
Let's try using these. So let's first just create a new user, user equals new User. Now I have an instance of the User class, $u->first_name, make it Jerry. And $u->last_name, will be equal to Seinfeld. $u->username is going to be equal to jseinfeld. All right, so now we've got one user.
Let's just duplicate that user. I'm going to copy those lines, and this one I'm going to make the variable that I use is going to be $c, and we're going to create George Costanza, and gconstanza. Okay, you can use any names you want, oops, it looks like I made a mistake here, I actually forgot username, there we go. Now I've typed it correctly. All right, so now I've created two instances of the user class, that's nothing new. We know how to work with those.
Now, though, I'm going to change this, so that instead it's an instance of the Customer class, right, it's a different class. Now, we're going to learn how to make changes to that Customer class so that it actually behaves different than User. For right now, it behaves exactly the same. It has the same properties and it has the same methods. And we can see that if we drop down here, and we'll just ask for $u and full_name, let's echo that. Full_name, that's a method, and put a br tag at the end, and then I'll just copy that.
Let's do it again, this time with the $c variable. So we're going to ask each of those to just tell us what their full name is, and we'll see that the subclass inherited that full_name method. Before we do that, let's also just add another one here for get_parent_class, and we'll do that for $u, and again we'll put that br tag at the end, and then let's do the came thing but for $c, see what it's parent class is. And then let's use that if(is_subclass_of, and we can use either a name or an instance.
It's more common to use an instance because we're really probably curious about a particular instance. We probably know about the structure of our classes and subclasses, and don't need to ask PHP to tell us. So let's see if it's a subclass of user, then we'll return back a line that says echo Instance is a subclass of User, and last of all let's try that class parents. $parents = class_parents, and we'll ask the $c instance.
And then let's echo that back. It's going to return an array. Implode, there we go, $parents, and we'll just grab this at the end and paste that in there. Okay, let's try all this out. Let's go over to Firefox, and the page that we want is class_inheritance. There we go, you can see they both have that full name method available to them.
You can see that when we ask for the parent of the user class, it didn't give us anything. It came back with a blank line. When we ask for the parent of the customer class, they came back and told us it was User. And yes, instance is a subclass of User, that returned true. And, when we asked for the parents of the Customer class, it returned back just User. Right, there's only one. If there had been more than one, we had several layers, then it would have bene a list of those with a comma between them.
So that's all there is to being able to define a subclass. Now, the reason to do this, though, of course is because it gets more interesting when we can start to customize the Customer class, so that it can make use of the parts of User that it wants, but it also can extend or override that behavior as well. And that's what we'll talk about in the next couple of movies.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project