6: Constructors and Destructors in OOP PHP | Object Oriented PHP Tutorial For Beginners | mmtuts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I gonna learn how to create something called a constructor and a destructor inside object-oriented PHP programming so in the last episode we had this example where we create a class called person I'm just going to go and continue with this example here since it's a pretty good example to follow when it comes to creating you know constructors and destructors so we talked about creating these different properties up here and we talked about just declaring the property without actually assigning a value to them now it is possible just to mention it because I didn't mention that in the previous episode it is possible to assign a value to one of these properties I could for example say 28 here and that would actually work if I were to reference to this property here it will actually have the value as 28 however if I were to do that it kind of breaks the functionality of why we would have to create different objects with different people in it because not all the people that I create from this specific class are gonna be 28 years old so it doesn't really make sense to do it that way you could if you wanted to to have a placeholder so if I want to say undefined or something as a string or something I can do that if want to have that as a default thing in case we don't actually set a value but its general just a thing that we do where we just declare them and then we assign values to them when we actually instantiate the class and create objects so when it comes to creating a constructor and a destructor what these are are a method that gets run right before we create an object and right after we are done creating an object so the constructor is run at the beginning and the destructor is run at the end now do bear in mind we don't always need to run a destructor but in a lot of cases you will be using constructors when it comes to creating objects so let's go ahead and create a constructor here so I'm going to create a public function and it is important to mention that the name we're going to give this function here what this method has to be what I'm about to write here otherwise it's not going to get seen as a constructor do bear in mind there are built-in methods inside opteka into PHP just like they are in zero PHP so there are just some examples like with constructors and destructors where you have to call it that specific name for them to work like they should so what I'm going to do is I'm gonna write underscore underscore there's two of them and I'm going to call this one construct and I'm just gonna close off the parenthesis and semicolon so whatever we write inside this method here will get run at the beginning when we do actually create the class and the reason we want to create this constructor is because now right before we create an object we can actually assign the values inside these properties we have up here this is a concept that I know a lot of people back many years ago when I was studying at the University had a really hard time understanding why they should create properties without assigning values to them and that's because later on when we actually create objects based off the class then we assign the values to that specific object so in here what we're going to do is I'm first of all going to reference to this class then I'm going to point to a property and this one is going to be named and I'm going to assign it a value which is going to be equal to dollar sign name and just like with the last episode down here dollar sign name is not a reference to this one up here it's a reference to later when we do actually create an object based on this class that after we instantiated it we can actually fill in parameters and fill in the properties so this dull son name is a reference to the parameter up here okay that's a little hard for some people to understand that's why I'm putting so much empathy and emphasis on it so so what we're going to do is we're just simply going to copy paste and we're going to do this until we fill out all the properties or at least the ones we need to fill out when we create a new object and I'm just gonna go and change the names here so we're gonna say eye color age then of course change the names of the properties over here so I color and age and there we go then we need to pass them up here inside the parameter or otherwise this is not going to work so we're just gonna pass into eye color and we're gonna pass in the H so what happens now well if I were to go inside my index file and I still have the stuff from the last episode we're just gonna go and delete most of it let's go and delete all the way up to when we create an object based off this class you may remember I said in the last episode you could choose whether or not to include the parentheses when you don't have a constructor now that we do have a constructor we do need to have parentheses here otherwise it's going to give an error because you have to pass in the data for these constructor properties to we're setting so what I need to do here is I need to set a name so I can say this one is called Daniel I can set a eye color which is blue and I can set an age which is 28 so now that we created this object it now has all the properties filled in when we create the object which is kind of nice so now we can actually start using the methods properly inside our class here so what I can do now is I could go ahead and say I want to reference to this object that just created and I want to reference to name and simply print that out so we're actually going to echo this echo this specific thing and I do still have the stuff from the previous episode inside the browser just don't look at what I have here so when i refresh you can see we have Daniel okay so going inside our index file here again what I could also do is a grunt o'clock our next piece of data just to prove that it is in fact all the different properties that we're setting in here so I could say I color and then I can go and refresh and as you can see even get blue now if I were to call on this method that I do have in here from the last episode called set name we can still change the name that we have inside here so what I can do is I can go ahead and say well we want to run a person 1 which is over here and I want to run a method called set name parentheses and set a new name so I can call this one John so now if we were to spit out this exact same thing here again which I have up here called name you can see that now we're gonna have Daniel blue and with you actually happy uncut reference era what did I do wrong oh I used a punctuation yeah I'm getting used to C sharp so one PHP now we're not using a punctuation to reference to to a method so if were to go ahead and reference the proper way using the arrow then now four words to go inside the browser refresh you can now see we get John so now forever after this code the name property is going to be set equal to John that's important to note so this is how we can would you call use of a constructor in order to create the different properties when we actually create an object based of this class here so let's talk about D structures now these structures are very similar to to the constructor we have up here it's done the same way we create a public function called distort and you might be asking what do we need to use to a specific thing for that's not a semicolon that sikozu bracken what we use a destructor for is simply to clean up our class a little bit we can use it to close off connections to databases we can do a couple of different things inside a destructor essentially if there's something you want to get rid off to clean up the class a little bit or clean up the object once you created you can do that using a destructor but really like I said the thing that the destructor actually does is that it goes head and runs this function when the optic gets closed off and now you might be asking well what exactly does the destructor run inside the code here is it when I instantiate a new class or is it after I actually call on this or what needs to happen here for this to actually can run well if it were to go back inside the index page the constructor and destructor both get run when I instantiate the class up here it's not something to get run after you're done using the optic down here so do keep that in mind now the last thing I want to show you just before we end this episode off is something I should probably also have shown in the last episode when I want to use any of these properties inside the class I'm inside out right now so let's say I go down inside my class down here and I'm just gonna go and create a new method I'm not gonna I'm just going to delete the destructor here and I'm gonna go and create a method called get name now what I want to do here so just simply want to get and return the name that we have as a property up here and you might be asking well how are we going to reference to that because right now it is set to variable name inside my class do I just copy this and paste it in here and then simply return it because that might be how some people would might want to do it but do remember we're talking about properties we're not talking about variables mean that if we want to return this property from inside this class here I do need to reference to this class and this property here so this is how we actually do it whenever we reference to a property inside the class this is how we reference to the properties okay so doing this we can actually use the getname method let's just go ahead and delete all these and really in a lot of cases just to mention it we talked about scopes people like to put these as privates which means that we can't reference to them inside our index file now because to set to private the only place we can use these properties is inside the class so in a lot of cases people like to instead set them to private and then use a method in order to return the values because this method is public but the properties are private so we can actually return the properties using a method if that makes sense so let's go ahead and return this property here go inside our index page and I'm going to reference to person 1 and I'm going to reference to a method called get name and because just returned we need to actually echo this line of code so we do need to run an echo and if we were to go inside you can actually see we get Daniel because I am now returning or actually running the method that actually grabs the property and gives it to us now that I set everything to private inside the properties let's actually go ahead and run the name property just to show you it will actually give me an error because it is private we can't access it so just to show you a habit that people like to do like I said people like to put these as private because this is maybe sensitive data in some cases right now it doesn't really matter that much just talking about a name I color and age but had it been personal info that have to be kept safe some people like to keep these as private and then we like to simply reference to them inside methods if we do X need to reference with them anywhere inside our class so this is the only one to say when it comes to constructors and destructors I do hope you understood what I said in this episode I feel like I was talking a lot about random stuff but I do hope that you can sort of understand what I meant when I said the different things you will be using constructors quite a lot when you use objects and classes so if you didn't quite catch what I was saying about constructors and do recommend that you watch this episode one more time just sort of get the idea behind it so I hope you enjoyed this episode and I'll see you in the next one [Music] [Music] [Music]
Info
Channel: Dani Krossing
Views: 76,721
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, constructor, destructor, php constructor and destructor, php constructor function example, php constructor explained, php constructor, php destructor
Id: ToomaGjgqBw
Channel Id: undefined
Length: 12min 36sec (756 seconds)
Published: Sun Jun 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.