PHP Front To Back [Part 20] - OOP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys welcome back to PHP front-to-back I apologize it's been a while since I did my last episode I've just been really busy and I couldn't get to it so today what we're going to be doing is we're going to work with object-oriented programming in PHP alright so we're going to create a class we're going to create methods and properties in the class instantiate objects and so on alright so I'm trying out this new FaceTime or face cam whatever you want to call it it was recommended to me by a few of my subscribers they suggested that I should do it one for copyright issues if you guys have been following me you know I've had some issues with copyright with my videos and then another reason is just to be a little more interactive and and have it help explain things or help me explain things to you alright so we're just going to give it a shot you guys can let me know if if you like it if it helps you or if it distracts you or from just too damn ugly to look at either way all right so let's go ahead and get into it now we have our PHP sandbox folder here and I'm going to create a new file and we're going to just say this is oo P dot PHP okay object-oriented programming and this is only this is the only file that we're going to need for this lesson all right now you might see me glance over to the right that's because I have my code over here that I'm you know my reference code so that's what I'm doing if I'm looking to the right alright so let's go ahead and create a class ok so we put our PHP tags and this class is going to be called person ok so it's going to represent a person object so in the class you can have properties which are basically attributes in the form of a variable and then we also have methods which are pretty much just functions inside the class so let's create a couple properties so let's create name and email ok we'll keep it really simple now when you have these properties you can you could add what's called an access modifier and that could be public private or protected now public means that we can access this from anywhere at all outside of the class if we set it to private we can only access it in this class and if we set it to protected we can only access it access it from this class and then any other classes that extend this one okay so hopefully that makes sense so far but I'm just going to make them public for now and then this small amount of code here is enough to actually create a person object so what we'll do is create a variable it's a person one and we'll set that to new and then the class name person and that's actually in this is called instantiate okay we're instantiating a person object so let's save this and then we're going to go and open up a little host PHP sandbox and Hopi dot PHP now notice nothing's happening because all we did was instantiate the object we're not doing anything with it so since these are public we can directly access them so we could go person one name equals and we'll set that to John Doe okay so in PHP when you want to access a property you want to use this this arrow this - and and greater than side if you use the JavaScript it would be like person dot name okay of a PHP uses that syntax so that's setting it if we save and reload we're still not getting anything because we're just setting a variable basically if we want to echo it out we can say echo person one name and reload and we get John Doe okay so this isn't very good practice usually what you want to do when you want to interact with properties is make them private and then create a public function to reach in and mess with them change them and so on so that's what we'll do we'll set these to private and let's go ahead and try and run it now and you'll see cannot access private property person name okay so we can't directly access it so what I'll do is just comment those out and then we're going to create a couple methods so these methods are called getters and setters okay so we're going to create one it's a public function get name and that's going to return this arrow name so this is the keyword we use when we want to reference something from within the class so we can access properties with this as well as other functions so we could do this get name if we want it to as well but so that's just going to return it or get it now to set it we're going to set public function set name and then that's going to take in the name and then we're just going to say this name is going to be equal to the name that's passed in so now we can go down here and take our person one object and we can do set name and tap in John Doe okay so that will set it and then to get it we'll just echo out person or person one get named okay now if we save that reload we get John Doe okay so it's reaching in grabbing the name and we're echoing it out and then what I want to do is just create it create a setter and getter for email as well so this will be set email and then this will be get email all right now you may not want to have to explicitly call this set set name and set email and all that especially if you're working with a lot of different properties so what we can do is we can create something called a constructor that is a method that runs when the object is created and you can actually pass things into it and you can do your sets there so for instance to create a constructor we want to do public and public function and double underscore construct okay as of php5 that's what we do back in PHP 4 we would have to create a function that matched the name of the class but we don't do that anymore so in the construct this will run when we instantiate the object so just to show you let's say echo person created ok and then let's go down here and I'm just going to comment out these two so basically all we're doing is instantiating this so let's save and reload and it says person created because this actually runs when we create the object all right so hopefully that makes sense now we can also pass values in to the constructor so let's say we want to pass in name and email and then what we want to do is just set them just like we did here so I'm just going to grab that and then email ok and then down here when we instantiate this person we want to pass in some values so we'll pass in John Doe and his email and then let's go ahead and do the getname save that reload and we get John Doe alright let me just put a line break here so I should put a line break on the gets as well so oops all right so let's reload that and now they're on separate lines so we didn't have to do the set name we just created the object and passed in the data now we also have what's called a magic method magic property or something like that and there's one called class okay so we can actually get the name of the class so instead of saying person created let's go ahead and do double underscore class double underscore and then just concatenate space created okay if we reload we get person created because this is this is going to give us the name of the class all right so that is the basics of creating a class and properties and methods we also have a Deconstructor we can do so if I copy this and then I'm going to just get rid of this stuff and we don't want to pass anything in and let's say class destroyed okay so if we save that reload cannot read the clear person oh this needs to be deconstruct okay and let's say deconstruct what a friggin idiot destruct alright so now person created when we instantiate it we were shooting out the name right here get name and then when it's when it's done okay we get person destroyed all right so now what I want to get into is inheritance okay because you can have classes that can inherit properties and methods from other classes so let's go down under the person class I'm just going to comment all this stuff out and we're going to create a class called customers and then we just want to do extends person okay so it's going to extend the person class and from here let's say that we want customers to have an extra field so they'll have a balance so let's say public actually will do private private balance okay and then we can have a getter and setter for that as well so I'm just going to grab that and then this will be set balance and we'll save this balance and then get balance all right now as is we should be able to create a customer if we say let's do customer 1 equals new customer and we're going to pass in a name and an email and let's save that and reload class customer not found Oh customers wait a second oh yeah that should be customer nos ok so we get person created person destroyed so since it extends person it's actually taking taking in the name and email setting it up here we're getting this created and destroyed now if I want to pass in the balance of say 300 that well let me just actually just echo out there's an echo out customer 1 and let's do get allons okay so you can see we're not getting it's not giving us 300 the reason for that is because if we want to be able to pass it in here then we need to also create a constructor in the customer class so public function double underscore construct okay and we're going to pass in here the name the email and balance and then we need to call the parent constructor okay so to do that we need to say parents and then this double colon underscore or double underscore construct and then pass in these three things to that as well all right and then right under that let's just do an echo and we're going to say a new a new and then we'll concatenate class like that has been created all right so now let's Glen save and reload and we get a new customer has been created and see get balance return this balance why isn't that oh we didn't set it in there in the constructor we need to do this just like we did up here and the constructor with name and email alright so that should work save it reload and you can see we get 300 let's actually put a line break right here alright so we have our person class which has a name and email and then our customer class extends it and adds a balance to it so that's pretty much it when as far as you know the fundamentals of object-oriented programming in PHP one other thing that I want to show you is the ability to use static properties and Static methods and what that means is that you don't you don't need to instantiate the object to use them so for instance let's say for person we want to do excellence to public for now we'll say public static and then let's do an age limit property okay so age limit we can actually set it here so we'll set the age limit to I don't know 40 I'm not even sure what is it when it's a limit for but we'll set it to 40 now we should be able to access this property without having to instantiate a person object so let me show you what I mean I'm going to just comment this off down here and then I'm going to go right under the person class and we're going to say echo and then the name of the class person and then this double colon and then the value on the property which is age limit so money sign age limit so let's see what that gives us we reload and we get 40 okay so we're getting the value even though we didn't even instantiate the person object it's static so we can just you can just use it it just works now we can do the same thing with methods so if we were to change this to private static and let's say we wanted to create a static method to get that value then down here we'll say public static function and this will be you'll say get age limit and then that's just going to return and when we're using static properties and static methods we don't use this so we don't do this age limit we use self okay so we do self and instead of an arrow we use the double the double colon okay so self and then age limit is what we want to return and then down here to call it we can just do person get age limit and that's a function save that reload and we get 40 okay so this is actually you know what I want to keep that in there for you guys so keep that and that's alright so this is static props and method alright so that's going to be it guys for this episode hopefully you enjoyed it and please subscribe if you're not already please leave a like leave a dislike if you didn't like it also let me know if this FaceTime thing helps FaceTime face chat I don't know whatever you want to call it alright so that's it guys thanks for watching and I will see you next time
Info
Channel: Traversy Media
Views: 53,468
Rating: undefined out of 5
Keywords: php, php oop, oop, object oriented programming, php tutorial
Id: FhbP6bF21Cs
Channel Id: undefined
Length: 18min 38sec (1118 seconds)
Published: Thu Apr 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.