8: Static Properties And Methods In OOP PHP | Object Oriented PHP Tutorial | PHP Tutorial | mmtuts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this episode you can learn about something called static properties and methods inside off to go into PHP programming now when we talk about static properties of methods this is a soft ik that a lot of people get very confused about when they need to use static properties of methods you might be very tempted to keep using statics instead of simply using objects that you can create from classes and it's very important me to point out when to use which you know when to use aesthetic and when to use an object that you create from a class when we talk about static properties and methods essentially we're talking about going inside a class creating a property or creating a method then we can simply access without having to create an object first and I might be thinking wow that sounds like a good idea you know property is a methods we don't have to worry about allocating memory instantiating the class creating objects it's so easy we just simply reference to them but no because static properties and methods are used differently than properties of methods are inside classes so let me go ahead and explain the differences here now the thing about using statics is that you need to ask yourself do I need to call this method or this property without creating an object first because we have to remember what is the purpose of creating an object well the purpose is is that the class is a template and in some cases I might want to create many different objects of many different people I might create a Daniel I might create a Timmy at John Alisa as something you might have a lot of different people we want to create here so if I were to take a look at this class here it makes sense that all these people all these objects I create from this class might have a different name they might have different eye colors different ages these different methods here will then reference to these properties that all the different people have therefore these also need to be irregular methods but in some cases I might want to create a property or method that isn't directly linked to creating an object from this class so kind show you what I mean here let me go ahead and create a static so if I were to create a static property inside this class called person I could say that we have something called a let's just give it a public static we use a static keyword here to make it into aesthetic and I might call this one drinking age and set it equal to something like 21 because I know in the US the drinking age is 21 and since a lot of my views are Americans just gonna go ahead and call it 21 and then Mike we don't have a drinking age I know in other countries it's 18 some even 16 but put American people drinking it is 21 so setting the drinking age to 21 is going to be a universal thing for all the different people that I create this object of it doesn't matter if it's Daniel Timmy or Lisa all the people have a drinking age that is set to 21 that means we can create this into a static because it's not essential to make this part of an object if that makes sense so what I can do here now is I can actually go inside my index file and instead of simply creating an object actually if I were to create an object I can't access aesthetic property from inside the class because it's not part of the object that created so we do need to not create an object in the first place I don't have to I just simply needs to create a reference to the class which is person right : : which is how we instead of the pointy arrow we used in the last episode we use : : to access static properties and methods so I can use : : and then type the name and in this case we do need to reference to the variable because it's not actually a property of an object so we do need to use the variable symbol in front of year and I'm gonna reference to drinking age so now for words actually echo this inside the browser you can see we get well we get an error now I did actually forget to put a variable symbol in front of my static property so make sure you remember to do that if you did good for you I just didn't do it so putting that in front of here let's go and go inside the browser refresh and as you can see we get 21 so we can actually access this property without having to create an object first which is kind of neat so what we can do is I can also create a method that is also a static method so I can go and create this one down here can say we have a public static and I'm just gonna go and call this one well we need to say function first but then we're gonna give it some kind of name I can call this one gets actually no let's set a new drinking age so set drinking age parenthesis curly brackets now when I need to reference to a static property I can do it inside either one of the regular methods I've been here we can also do that because it's static which means that we can access it from anywhere so I can do that from inside the get name regular method if I wanted to but we can also go down inside the static method which I can also access from anywhere without having to create an object first so what I can do here is I can write self call on call one and then I can write the name of my property so in this case it is drinking age and what I'll do here is I've gotten pass in a parameter so I'll say want to pass in new dr4 drinking age that's a that's an a so new DEA so kind of cut it short there and then I'm just gonna go ahead and set this one equal to the new drinking age so what I can do is I can actually go inside my index file I can say we have person call on colon and then I can say they have a static method and this one is called new drinking age parenthesis and I can simply pass in a new age so let's say we're somewhere else in Europe it is 18 and now if we were to actually echoed this out one more time you'll notice that we changed it to a teen whoops we have another another error ah okay yeah we need to make sure we use the same name of course for the aesthetic method so if it were to go in here it is not new drinking age it is set drinking age okay I'll make a lot of errors in this episode but I think it's okay cuz a lot of you guys are pretty smart so you catch a lot of these errors so while I'm making them you fix them so as you can see we now have 21 and we have 18 because I'm echoing it out before we change it and after we change it so just to show you that's actually going to go back here because I want to assure the whole we can access stetic properties from regular methods as well so if I were to create a new object based off this class the static properties of methods are not going to be part of this object but they don't have to because we can access them without needing to create an optics from them and you might be asking well why do we put them inside this class here if they don't become part of an optic when we created it's just simply because we categorize it so right now we have a class called person and in my head I think that the drinking age should be part of the person class because it kind of belongs under that category so I can actually put it inside this class and have it in here together with the properties and methods that gets created when we create an object so don't get confused about the fact that we mix to get the statics and non statics inside classes they are two different things essentially but they're just put together because they are categorized under the same class name okay so what I can do here is and go inside get let's not get the name but instead let's get drinking age so get da and I can go ahead and instead of returning this name because this is a property and what we want to get is a static property is I can garden again referred to self which again is another keyword that references to this class instead of using variables this when it comes to aesthetics we use self for reference to self and then what is it called because this time I want to get it correct without arrows please so we're going to set it to drinking age and women returned this I can go inside and reference to get da just gonna copy it so don't make a mistake again and I'm just simply gonna get the get da function or method inside the class so every words actually run this go inside you can see if we get 21 so we can access static properties from regular non-static methods in order to get some values here so with all this said I think this is pretty much what I wanted to show when it comes to statics and I hope that you understood the purpose behind the statics because a lot of people like I said get confused about windy view statics and when do we use non-static like should we just make all the properties and methods inside a class a static method because then we just access it from anywhere we don't need to create objects first but that's the kind of attitude that tells me that people don't understand what the purpose is behind creating objects so make sure you understand that aesthetic properties and methods are used when you want to create properties and methods that don't have to be part of an object like it doesn't have to be in there so then you can make it static okay so this is all I want to show in this episode I hope you enjoyed and I'll see you in the next one [Music] [Music] [Music]
Info
Channel: Dani Krossing
Views: 66,051
Rating: undefined out of 5
Keywords: static properties and methods, static properties and methods in oop, what are static properties and methods, static properties in oop, static methods in oop, static modifier in oop, oop php static modifier, static modifier in oop php, what are static methods in oop, what are static properties in oop, static properties tutorial, oop static methods tutorial, oop static properties tutorial, static properties and methods in object oriented php programming, php tutorial, mmtuts, php
Id: L9jLxuLAAIY
Channel Id: undefined
Length: 10min 23sec (623 seconds)
Published: Wed Jun 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.