Object Oriented PHP #7 - Inheritance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
our other gang so the next big topic I want to discuss is inheritance and inheritance is actually a pretty simple concept in programming once you get your head around it in a nutshell inheritance is when one class inherits properties and methods from another class a bit like in real life a child inherits properties from a parent like the eye color or the skin color or something and it's the same in programming we can have one class inherit things from another class so say for example we have some new class that we've created and we want to inherit from another class that we already have so that that new class has all the same properties and methods as the class its inheriting from now that new class could also have additional unique properties of its own and an example of this would be take our user class let's create an admin user which inherits from that user class the admin user should have all the same properties as a regular user username and email it should also have the same method add friend but it should also have an additional method or props of its own for example a method to remove other users or property to define the level of admin that that user is now if we were to do this we could create a brand new class from scratch and add all of the same properties and methods that a normal user has to it pause the extra methods but this is going to lead to code duplication and that's going to make things harder to maintain in the future so instead what we could do is inherit the properties and methods from the user class so we could have our user class and then say ok well we'll create this admin user class and that is going to inherit from the user class therefore I'm going to take all of the current properties and methods on this class and I'm going to automatically inherit them as well so we don't need to redefine them on this new admin user class but I also want that extra property down here admin level so I'll just add that to this class and therefore when we create a new user using this class we're creating a new admin user object and it's going to have these original properties that all users have and that the admin user inherits but also this additional admin level prop see right here so that is inheritance in action now if we wanted to more than one class can inherit from another class for example we could also have a third class called moderator which also inherits from the user class and that means it's going to have all the same properties of methods username email and ad friend but also we could define an additional method called report user on this moderator class and only moderators would have that so any object created with this class would have all these properties and the report use a method now another way of saying all this is that a parent class could have multiple subclasses on multiple child classes so it called as a parent and maybe a child or a subclass so now we know a little bit of the theory behind inheritance well let's have a crack at this in our code ok then so let's try creating a new admin class which inherits from this user class right here so the first thing we need to do is define that class so underneath this class I'll say class and then admin user you can call it just admin if you want to I'm going to call it admin user and then when we inherit from another class we say what we want to inherit from by saying extends and then which class where inheriting so that's going to be the user so we're saying here that the admin user class is going to extend the user class and therefore it's going to inherit all of this stuff inside the user class the properties and also the functions so what if I now come down here and say well ok now we have user 3 and that is going to be equal to a new admin user this time so this time we're creating a new instance of this class we still want this user of this admin users have a username and an email so we'll pass those in so let me pass in Yoshi and then Yoshi at the net ninja could it you care like so because again the admin user is going to have all of the same properties and methods as a regular user so we're passing those properties in and we want them to be set now what's going to happen here because we don't have it construct a method inside this class right here so when we make a new Bing user it's looking at this and it's not gonna find a constructor but that's okay because when it creates this admin user it looks in here and it doesn't find a constructor and then it says okay well does this extend or inherit from another class and it does so it goes to that class instead and it fires the constructor inside here where we take that user name we take the email and we set those properties so this is still gonna work even though we don't have a constructor inside this class and I'm gonna demo that I'm gonna say now user 3 and what we'll do first is get the username and we need to echo this so let me place echo in front of it echo and then we'll concatenate with a br tag so it goes to the next line and I'm just going to copy this dude right here and paste it down below a couple of times because now what we're gonna do is also get the email now remember we can't just say email like this because that is a private property we need to use this method get email which we inherit because it extends user so we can use that so I'll say get email and in fact that's all we're gonna do for now we're not going to do this third one down here so let's get rid of that and if I save this now and I'll refresh over here then we can see Yoshi and Yoshi at the net ninja quarry UK so this works we're passing the information in it's not finding a constructor inside this class so it goes to the parent class and it fires a constructor there it fires that and it sets those properties okay so now we're inheriting from this user class but at the minute this is kind of useless because it doesn't have any extra properties or any extra methods so what if we also want inside this admin user class an extra property called level so let's make this public for now and call it level and this will just be like the level of authorization this admin has ranging from I don't know zero to five or something like that so it will be an integer and what we need to do now is pass that property in when we say we want a new admin user so after the email we could say something like five because we want this to have a ninja level or rather an admin level of five so we're passing it in right here but at the minute it's not going to get set because what's happening is we're calling this to create a new admin user it's not finding a constructor it's going up here to set up those properties and right here we're not actually setting that level property because regular users don't have a level property so in order to do this we have to make a new construct function down here inside admin user to set that property so let's do that I'm going to say public and then function and then underscore underscore construct and inside here we want to take all three of these things we can't just take the one thing and call it level because now when we say we want a new admin user it's going to come to this class and it's going to find this construct function and it's going to fire that and it's going to take all three of these arguments in so we have to define all three of them here so I'm going to say first of all user name and then we also want the email and we want the level now inside this construct function we want to set this property here so I'm going to say this and then level is now equal to the level parameter we take in make sense and now we can try echoing that out down here so let me copy this and paste it down here so we want the level right here save it and I'll refresh and you'll notice two things first of all we get the level that's fine so we've set this property that we take him right here and now we can access that but these two right here they're not actually output anything and why is that well now that we have a construct function inside this class over here it's overriding this one so no longer when we create a new admin is it coming to this class and saying well okay you don't have a constructor so I'll use this one up here instead it's not doing that anymore it's finding a constructor function and it's only firing this one if we have one here it doesn't fire the one in the pairing class so we need a way to set these two properties now what we could do is we could create those properties down here again so public and then email and then you know public user name or private email and public user name but that kind of defeats the point of inheriting those properties so I don't want to do that instead what I want to do is find a way to call this up here from this constructor so that we set the level first of all then we call the parent constructor to set the other properties and we can do that the way we do that is by saying first of all parents and then double colon and then we want to call the construct method so this parent keyword right here refers to the parent class in our case the user class and then we do double colon and then we want to call the construct method so in here we can pass it through the username and the email because we take those in right here username and email so we pass them through to the parent constructor so inside parent constructor when it's called it sets those properties so now we have the level set and this set right here so if I save this now and try this it should all work refresh and we a fatal error says call to undefined method user construct okay I spat it incorrectly so let's call it construct not contract save that and I'll refresh and now it all works awesome so this my friends is how we could inherit from a parent class we say the new class extends from whatever parent you want to inherit from then we can define any new properties or methods inside this class we can also define a new construct function which is going to override the parent one but then if we still want to run the parent one we can do that using this line of code right here parents double colon underscore underscore construct and then pass in the original arguments that this constructor needs right here so now we've created that subclass in the next video I want to talk about how we can override properties and methods inside this class we've already seen the first case of that right here where we override the concentrate function but I'm going to show you some more examples in the next video
Info
Channel: The Net Ninja
Views: 19,201
Rating: undefined out of 5
Keywords: php, oop, oop php, object oriented php, objects php, php object, oop tutorial, php oop tutorial, php tutorial, tutorial, advanced php, inheritance, class, classes, php classes, php subclass, subclass, sub class, php sub class, php extends, php inheritance, php subclasses
Id: mRMFQP_98oo
Channel Id: undefined
Length: 11min 19sec (679 seconds)
Published: Wed Oct 09 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.