5: Properties And Methods In OOP PHP | Object Oriented PHP Tutorial For Beginners | mmtuts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the last episode you learn how to create classes inside our particular into PHP and in this episode we're gonna learn about something called properties and methods when it comes to PHP so as you can see in front of me here I have a basic index page there's nothing really exciting going on in here besides maybe a couple of PHP tags we're going to use for something a little bit later on other than that I have a person that Inc the PHP file which is where we're going to create our class and in this case I created a class called person so what we're going to be doing here is we're going to talk about these properties and methods and what exactly they are and how we actually use them when it comes to PHP so when it comes to using classes inside PHP the thing that we use it for is to create something called objects so at least that's one of the purposes behind creating classes and other purpose is that we have a way to categorize our code inside our project so any sort of methods or properties that I have that is regarding a person I can put all of that inside a class called person so we have a categorized inside one place so what we can do with these classes here is that they function as a type of blueprint or a template and based off this template we can create many different copies of this class so you can create an object that has all the properties and methods of the class that is just a separate object to give an example here let's go and go inside the index page and what I'm going to do is I'm gonna go and create a couple of different variables I'm going to say we have a variable that is called name I'm gonna set this one equal to Daniel then I'm gonna set something called a eye color just to have something I'm gonna set this one equal to Lu cuz my eyes are blue and then we're going to go and set an age we're gonna say age is equal to 28 so now we have a couple of different variables and this could be properties of a specific person we maybe have some functions that could be regarding this person so we could have a function that goes in and updates the age after a year has passed so we can do a couple of different things with this person however if I were to go down and say well I have this other person called I don't know Timmy and I knew to copy-paste the same variables again because now we need to have two people and suddenly now we have to give this guy a number as one because we need to have multiple people so we gotta somehow categorize these different names and properties so I'm gonna go to name these ones too and now we start to make things a little bit messy because now things are not making sense and do bare mind if I want to use this code right now I'm using it inside my index page we could of course put inside a separate document and then link to that document on all the pages but it just kind of makes sense if we want to categorize these under a specific class so we don't have to repeat our code again and again and again therefore when it comes to creating classes we can instead create something called an object and this is something we can just instantiate and then use undergo whenever we need to create a new person which is kind of handy so now if you were to go inside our class called person and I should probably mention that I am using Adam as a text editor because I know I'll get questions about that in the comments since this is not the one I use some some of the all the tutorials in this course here I did use to use sublime text this is the one called Adam 8000 M ok so inside this document here we have our class called person and what we can do inside this class is we can create a bunch of different properties that a bunch of different methods which kind of works like what you call it variables and functions but not really because we're talking about properties and methods that are part of a class which will become part of an object once we actually create that object so not really the same thing as variables and functions but if you don't have any experience when it comes to classes that's the closest thing I can sort of related to if you have to understand inside this class here what I'm going to do is I'm going to start creating some properties so just to make comments properties and then I'm going to go ahead and create a property which could be a pup League because you talked about scopes at one point we're gonna create a public property and I'm going to call this one name now as you can see I only declared this property inside the class I didn't actually assign anything to it and that's because one of the functionalities the classes has when we want to create an object is that we can assign values these properties when we create the objects so simply declaring the properties is something that you should get used to doing when you want to have some data that will change depending on which person we're creating or which objects were creating based off this class here so I'm just gonna go and create a declared property without assigning anything to it and if it doesn't make sense now it will make sense a little bit later so just stick with me and then you'll actually see what I mean there so what we can do is we can also go and create a property called eye color we can create one called age and that's pretty much it so now that we have these properties let's go out and create a method because that would kind of make sense that since we're talking about properties and method I don't create a method there so we're gonna go and create a method and we're gonna call this one a public function and then we need to give it some kind of name we could actually go ahead and sit what one of the values for one of these properties is up here just for fun for now I know I said that when we create the class we need to create the variable or not the variables but the the values for these different properties but just for fun for now let's just go ahead and create some of the values for these properties using a method just for fun so inside this function yeah I'm gonna go and set it to or call it some kind of name I'm gonna call it set name just to set some kind of name up there parenthesis and curly brackets so in here what I can do in order to point to one of the properties I have inside the class here is I can actually go and use a variable this keyword which points to this specific class where inside of right now and I can then use this arrow which is a dash and an arrow now I'm actually in doubt is it called a dash in English I'm not quite sure that you might correct me in the comments or something but that little line that connects two words together that's the one you got to use so a dash greater than and then you need to refer to the property that we have up inside our class now let me do this we don't actually write variable name like so we actually just write name because variable name is already a property inside the class and the name for this property is name not the dollar sign in front of it so we just need to refer to the name not variable name then what we can do here so we can actually go and set it equal to variable name and now you might get confused because all I'm doing here I could set this one too that's a new name or something it doesn't have to be the same variable but what I'm essentially doing here is I'm just gonna grab a new piece of data by passing it by passing it in inside the method here so when I actually use this method inside my code inside the index page I'm just simply gonna pass in a name inside the method parameters and then it's going to assign itself to this property inside the class that's essentially what we're doing here so what I could do is I could actually also go and do this if that makes more sense data but a sense of we're just passing in the data from the method even so what I can do now is I can go inside my index page and in order to use this class inside our document here we actually need to instantiate the class first now when we create any sort of object from a class we need to allocate memory for that specific object and we do that using a keyword called new so if I were to go in here first of all we need to actually create the object the phone can use anything inside the class so I'm just gonna go and create a variable I'm gonna call it some person one because we're just creating a new person here and I'm going to set it equal to new and then the name of the class which is person and now you can actually choose at least as it is right now whether or not you just want to close it off or if you want to use parentheses I recommend always using parentheses because we will talk about something called constructors at one point which will require that you use parentheses because need to pass in parameters this is previously when I talked about that when we create an object we set the properties this is what I'm talking about using constructors so just make it a habit to use parentheses but you can dude without if you want to but make it a habit so going below here what we're going to do is I'm going to take this a new object that is created called person 1 and I'm going to access one of the methods from in Sabich because I need to set a name property in here actually before we use the method that's actually going to just echo out the name property from inside the class so if I were to go ahead and say we want to echo a dollar sign person 1 which is the object we just created and I want to link to the property so we've got a point two and you have to do this whether we link to a method or a property we have to use this little arrow here and I'm going to go and point to a property called name notice that I'm not using the dollar sign because like I said we're talking about a property not a variable so we just need to write name inside what we have here now before we can do anything inside the browser we do need to link to our class file because right now just gonna give us an error if we were to go inside the browser and refresh so we need to go ahead and link to our class file which is inside a includes folder so I'm gonna say includes forward slash and I'm gonna go ahead and say person Inc dot PHP so forward to go inside the browser now and refresh you can see we get nothing inside the browser that's because we haven't actually set the property inside this optic we just created so if I were to go ahead and say that we want to use our set name method right before we do this I can actually go ahead and say well let's just go ahead and run the method called set name parentheses and then we need to pass in the name that we want to set to the property called name so in here I'm just gonna go and write Daniel save it go inside my browser refresh and as you can see for what assuming of it we now have Daniel because Daniel is now set equal to the property called name inside our object here not inside the class but inside the object that is a copy of that specific class so with this said this actually couldn't create another object because like I said a class is a template and then create as many objects as we want from this class so if we were to go and say I want to create person two in this case here I'm just gonna go ahead and Stan she ate the class one more time so we're gonna say we have a class called person parentheses and then below here I'm just gonna go ahead and set a new name for this class actually let's go ahead and echo out the property called name because what do you think this is going to end up being of course need to change step two - when I echo this out inside the browser so you might think is it gonna say Daniel how's it gonna say nothing because if I were to go ahead and do this it's just going to delete that previous echo otherwise we will get Daniel inside the browser if I were to go inside the browser refresh you can see how we're not getting anything but that's because right now we created a second object and we haven't set a value for the second object Daniel is part of the first object not the second one so what I can do here is I can go ahead and run this method again right before we echo it out cause I'm gonna refer to the second object and I'm gonna go ahead and set the name to something like Timmy then if words echo out the name you can see we get Timmy let's just go ahead and and call both of them just to see both names inside the browser so let's echo out object number one and object number two at least the name property from them now as you can see we get Daniel and Timmy so now with these objects created the property inside these objects will now be Daniel and Timmy and we have actually set those values so what I need to do later on if I have to create a let's say I have to use these properties for some other method I create inside this object I don't have to set the properties again because they are saved now forever as Daniel and Timmy with these two objects here so unless I destroy the object they're not gonna change and that's kind of the cool thing when it comes to creating objects from classes when it comes to optical into PHP so with this said I don't think there's anything else I want to teach you for now when it comes to simply creating objects and creating properties and methods but in the next episode we're going to get more into these constructors and destructors that are talked about that were pretty much an essential part of creating classes when we want to fill in values inside these properties we have inside the class so in the next episode we're going to talk about talk about constructors and destructors so I hope you enjoyed this episode and I'll see you in the next one [Music] [Music] [Music]
Info
Channel: Dani Krossing
Views: 82,961
Rating: undefined out of 5
Keywords: Object Oriented PHP Tutorial, oop php, php oop, php, oop, object oriented php tutorial for beginners, mmtuts php, php tutorial, learn oop php, learn php oop, learn php oop tutorial, php programming, php programming tutorial for beginners, php object oriented programming tutorial for beginners, mmtuts, properties, methods, oop php properties, oop php methods, how to create php methods, how to create php properties, php properties, php methods
Id: U-SIguEOk0o
Channel Id: undefined
Length: 14min 10sec (850 seconds)
Published: Wed May 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.