4: Visibility And Inheritance In OOP PHP | OOP Scopes | Object Oriented PHP Tutorial | PHP Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the last episode you learn how to create classes inside opticore into PHP and in this episode we're gonna talk about something called visibility and inheritance now it's going to be kind of weird because I can't talk about visibility without also talking about properties of methods which is not something I'm gonna teach until the next episode and I can't talk about properties and methods in the next episode without also talking about visibility because they kind of go together so I'm going to talk about both in this episode I'm mainly going to focus on visibility and inheritance here so if you have any questions about properties and methods just go and watch the next episode and see if you still have any questions there so as you can see in front of me here I have a couple of different documents I have a index page and I also have a class page inside the class document I have two different classes I have a person class and I have a pet class and I don't think I mentioned this in previous episode but it is possible to put multiple classes inside one document it's not the best way to do things but it is possible so just for the sake of this exercise see I just went ahead and took two classes put them inside one document so it's easier to see but what I want you to get from this is that we have two classes and inside these classes we have some data we have some properties inside the first class up here which is person a property is kind of similar to a variable it's not the same thing but the best way I can describe it is that it's kind of like variables inside an object if had to say in some kind of way again it's not the same thing I will get hate in the comments if I say they're the same thing they're not so but again for new people they're kind of like variables if you were to go down inside my pet class you can see we have a not a function because again I will kill hate if I say that but I have a method which is kind of similar to a function okay now the idea here is that from inside the index page I want to echo out the method down here and right now as you can see inside the method I have a variable that is set equal to high there and I simply return variable a so with this method here what I can do is I can go inside my index page and again you don't need to understand what is going on in here no if I explained that in the previous episode how to actually create objects I may have I don't know but just know that I created an object based off the class called pets which means that I can now use the properties and methods inside that object okay or inside the class so what I did is I echoed out the owner method that is inside that specific class called pet because I did name it owner if it were to go back in here can actually see that's the name of it and now to show you what exactly happens if I were to echo this out inside the browser this is what we're going to get and as you can see if we get an uncaught error hmm why is that happening well right now we have something inside our properties and methods that he may have noticed which is something called private now whenever you create a property or method inside any sort of class you need to define a disability for them now right now I've set them to private all of them because as a default you need to always make sure you first of all prioritize putting them as private whenever you create any sort of properties and methods and then you need to think to yourself hmm do I need to use these pieces of data outside the classes because when we set them to private we can only use the property some methods inside the same class so this method down here that I'm echoing from the index page doesn't have the visibility to actually access it since it's set to private it's only whatever's inside this class here that can actually access it so like I said we need to sort of think do I need to use this method outside the class in this case here I do need to use it outside the class because I'm trying to access it from inside my index page so I do need to have some kind of visibility that allow for me to access it from in here and we can do that by setting it to public instead so now for what you go inside the browser you can actually see that we now get high there because now we set the visibility to the correct one so we've talked about private and public as visibilities but we haven't talked about the last one which is something called protected when it comes to private we can only access the data from inside the class this means that if I were to actually take this what do you call this method down here it just copy-paste it up inside my person class then if I were to take this variable a and instead reference it to some of the data have inside this person class so I can actually just go ahead and delete delete delete now the way we reference to data inside the same class is using a keyword called variable this which means that I'm pointing to this class I'm inside of right now and what I can then do is I can point to a specific property of method using the little arrow here and I can point to a property called first now do notice that I didn't put a dollar sign in front of the first name because I'm pointing to a property another variable again like I said a property is not the same thing as a variable we just need to define them with the variable symbol when we create them but when we reference to them we don't use the variable symbol so if I were to do this and then tweak my index page a little bit so we instead of calling out the pet class we instead call out the person class because now we're referencing to the person up here so and if I were to do that it should now write out my first name so were to go inside the browser refresh you can now see it says Daniel and now you might have a few questions because why can I print this out inside my browser when the properties are set to private hmm this is going to take a look at what exactly is going on inside the class again so as you can see inside this class here we have some private properties and I have a public method now because private means that I can only access to data from inside the class if I were to make a public method like this one down here and actually axes the property from inside the method it now means that I can actually access the properties from inside this method here because this method can access the properties so if I can access the method from the index page then I can also access the properties from within the method if that makes sense so that's why we the ACOG the data here if they're inside that method so now let's go ahead and just go back a little bit here actually let's just go ahead and copy pasted because I'm actually gonna do something very similar here so if we were to copy Leeds and paste it inside the class we have done here and just replace it we're going to talk about the protected visibility so right now what we have here that Mexico and change it inside my index page as well there we go so right now what we have here is the same classes that we had as before we have person and we have pet now the pet method is right now still trying to access a property from within the same class but right now we don't have a property inside this class called first because that's what I'm trying to access here so what we can do is we can inherit properties and methods from one class to another class and we can do that using a very simple keyword so for were to go up inside the class name where we defined the class up here and say that it extends person it now means that all the properties and methods within paracin are now getting inherited into the pet class this means that I can now access these different but II call properties up inside the person class if it were to use the protected keyword so just to sort of explain this let's actually go and see what happens right now for were to refresh the browser do notice that these properties are still set to private so for were to go inside the browser this is a see we get an error message and as you can see we now get a undefined property which means that it can't find it so forwards go back inside my classes here what I can do is I can go ahead and make my private property up here inside the person class and I can actually make it protected instead so by doing this what I'm now saying is that this property up here can only be accessed from within the same class just like private however if any classes inherits any sort of property method from this class here we can also use some insight that other class this means that right now if a which X refers to browser I can access this data from within the other class and still print out the data and I hope that sort of makes sense to you because protected is one of those sort of visibilities that confuse a lot of people so just know if you have protected data then you can only access the data from within the same class or any class that extends to it okay so right now this class down here has access to all the properties of methods from within its own class and this class up here I should also mention that right down here when we do actually gain access to the property from within the other class I'm referencing to that specific property as variable this which has set before was when you reference to something within the same class but because we right now extend to that specific class up there is to kind of see them as the same class now so they're sort of like an extension of one another so we can still use the variable this keyword in order to access properties from within the other class as well now just the last thing just to properly demonstrate this I just want to go ahead and go inside my index page and what I want to do is I want to gain access to my person class and instead of trying to print out a method I'm just simply gonna print out the property call first like so which means that if I were to go back inside my class set this one to private we are now trying to access this private property from within the person class over were to refresh the browser you can see then we right now get an uncaught error because right now we're trying to access a private property so if we were to go back inside the class change it to protected what do you think is going to happen because remember we can only access this data from within the same class or within any classes that extends to it does that mean we can access it from the index page no does not so if I were to refresh you can see we can still not access it because it's protected I would need to create a method from within the same class or other class down here that then accesses the specific property and prints it out or echoes it out inside the browser if I were to set it to public however and then go inside the browser again you can now see that we can access the data because it's set to public so that's what I want to talk about when it comes to visibility and when it comes to inheritance at least when it comes to classes in the next episode like I said we'll talk about how to you know create properties and methods what everything means there and you hopefully shouldn't have any questions when it comes to creating properties and methods so hope you enjoyed this episode and I'll see you in the next week [Music] [Music] [Music]
Info
Channel: Dani Krossing
Views: 90,950
Rating: undefined out of 5
Keywords: php tutorial, mmtuts, php, classes, php classes, php auto load classes, spl_autoload_register, php spl_autoload_register, load classes automatically in php, use classes in php, link to classes in php, object oriented programming, object oriented php, object oriented php tutorial, object oriented php tutorial for beginners, php inheritance, class inheritance, php class visibility, class visibility, visibility in classes, class scope, php class scopes
Id: X6xY4Ouydas
Channel Id: undefined
Length: 12min 7sec (727 seconds)
Published: Tue Aug 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.