Lessons: 18Length: 2.3 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.1 Class Inheritance And Protected Scope

Hello and welcome back to PHP OOP Fundamentals. In this lesson we'll have a look at one of the key concepts of object oriented programming called inheritance. So, what is inheritance? Well it's a bit like with humans really. When two people with brown hair get a child, there's a fair chance their child will have brown hair as well. In OOP we can take a class and create a child class from it. When you create a child class from a parent class, the child class inherits all of the properties and methods of the parent class. Let's have a look at a very basic example. Okay we're in this folder here for lesson 12 and before we do any work let's first change the vendor name for our app. It's a bit daunting to keep staring at my own name all the time. So, I'll just rename that. A vendor name could be any name really. I could name it App or I could name it Fu or Bar. In this case let's just pretend we're building this app for a company called Acme. So, we'll just name this Acme. So, that was simple enough but now because I've changed the name for my vendor I also need to change all of my name spaces. So, I'll just do a quick search and replace. I'll search for the old vendor name which is Joseph Vane followed by a backslash and just replace that with Acme and a backslash like so. And that should have changed all name spaces. Let's just go into user and indeed that has changed to acme/app. So, just a quick check to see if our app's still running and indeed it is and as you can see we have an acme/app user object here now. So, that reflects a new name space. Okay, good. Back to inheritance. I would like to create a special kind of user. Maybe an administrator. So, I'll just create a class for that. In a file called Administrator.php. Let's make sure we give it a name space. Acme\App and that should do. Now all we have to do is create a new class and we'll call that Administrator but this time we'll use a special keyword namely the keyword extends and that should be followed by the name of the parent class. So, in this case that would be user and that's really all there is to it. So, let's see if we can make that work. Let's just open up index.php and clean this up just a little. Just remove this and this and here we say joost will equal a new Acme\App\Administrator() user, like so. And for the rest we'll just leave the codes we created it in the previous lesson so we have an auto loader here. We'll set some rules to validate input against. We'll have some data to fill the class with then we'll just run the validator like so and if the validator passes, then we'll create a new administrator and dump him to the screen. Now let's just check to see what we get in the browser. Now as you can see it's completely functional. As a matter of fact, I have an object of the Acme app administrator type here. It contains an email and a password, a fillable array, etc. Okay now remember when we created our administrative class and had it extend the user class the administrator class inherited the properties and methods of the user class. Well if that's the case we should be able to make our administrator log in. After all that's a method that is defined in the user or the parent class. So, I'll just go back to index.php, remove this line here, and just do $joost->login() and now if all is well that should dump something to the screen. That is if I precede it with echo. So, let's do just that. Okay. Just a final check and here we are logging in with a vengeance. So, but what if I want to extend the class from another name space? Well let's just try that with the user class in our Acme Library Folder which is over here. Will that work? Well we'll just append this with Acme/library. Save it out and have a look. Nope and that doesn't work. You see PHP is trying to load Acme App/Acme/Library/User.php. And like we saw before that's because its search is relative to the namespace we are all ready in which is Acme/App. Okay. So, what if we prepended this path with a backslash to make PHP search from the root? Let's see. Yep, that works. We're still getting an undefined method login error like here but we'll fix that later. Back to the class. I'm curious. Could we also use a use statement like we did before? Something like use Acme\Library\ and then user to see if we include that namespace here then we should be able to get rid of all this. Yeah, that works as well. Now we're still getting that undefined method error here because the login method doesn't exist. Okay let's just go back and open up our parent class which is library user and we'll just paste it in like so. And now all I have to do is alter this a little. So, login from user class or something like that and yes there it is. We're logging in from the user class. So, in retrospect we have a public method log in which is defined in the parent class and then we have a child class where that public method is not defined. But then if we instantiate that class right here and we call the public method from the parent class, then that actually works like so. Now we already talked a bit about the difference between private methods and public methods. I'll just clean up this mess here. Like so. Now what do you think would happen if I changed the scope from public to private here? Well the private keyword would make it so that this method would not be available outside of the class but what about it's child class? Would it be available there? Let's just find out. Fire up our browser again and refresh the page and there you have it. Even though this private method is defined in the parent of our child class it cannot be accessed by our child. Instead we use another scope keyword for that and that one is called protected. Now when a method or a property for that matter is protected it is not available outside of the class but it is available to its children. So, because it's not available outside of the class this would still throw an arrow. You see we're outside of the class now and we're trying to access that method and indeed that does throw a fatal error. You see we're calling a protected method from an invalid context. So, then what would happen if I were to create a public function say get login or something and then inside of that if I were to return a value and that value would be the result of this login? Well because this method is protected it's visible to the child class and therefore I can call it here. Well that should work. Let's see if it does and then of course before we do we need to call the getLogin method here. Okay? And yes. That works like we expected. Let's just turn it around again. Let's go back to the user class. Class. The parent class. Turn this into a private method again and now we're trying to access that private method from within the child class and that would throw an error. So, quick recap. Visibility scope for children classes are as follows. A private property or method is only visible to the class it is in and not to any child classes or outsiders. A protective property or method is visible to the class it is in. It's also visible to children classes but it's not visible to the outside and then a public property or method is visible to the class itself, to any children classes, and to the outside world. Okay and now let's just go back to the first scenario where our administrator class just extended the acme app user class like so. Now you may be wondering at this point why on Earth would you want to extend a class with a chance class? Well the answer to that question lies in the keyword extend. You can in fact extend on the properties and methods of the original parent class. So, let's just go back and open up our original parent class. We have a couple of properties here. They're now private. Let's just make these protected so they can actually be accessed by our child classes. Now we could for instance create a new property. Let's make that a boolean and it will be called isAdmin. Now this will tell our app whether we're dealing with someone with admin rights or not. So, for our normal users we would just set this to false but then for an administrator user then we could do something like this. We could maybe create a constructor like so and maybe set this is admin to true. Like so. Or maybe there's something that you would want an Administrator to be able to do that a normal user couldn't do for instance I don't know. Report for duty or something. Okay. Well that's all for class inheritance and particular scope. I'll see you in the next lesson where we'll take a closer look at class inheritance but from a slightly different angle. I'll see you there.

Back to the top