PHP OOP - Beginners Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this video is going to be on object orientated PHP alright so if you do not know any PHP at all I would suggest that you at least learn the very basics at least things like variables arrays functions loops just just the real fundamentals of PHP alright if you if you don't know PHP but you have experience in another language especially if it is if it's an object orientated language then you should be alright alright what we're going to be doing is going to be pretty simple so if you already know object orientated PHP then you probably don't want to continue because it's probably just stuff you already know but feel free to watch and maybe use it as a refresher alright so what I have is I already have my service set up a musings amp or xampp and I have my localhost at local dev and then I just have an index.php file so the first thing we're going to go over is classes ok classes are the basically the building block of object orientated programming at least in PHP alright so to define a class you just want to use the word class and then whatever you want to call it in this case I'm going to call it customer all right and I haven't already planned out this code just to let you know I'm just kind of freestyling it alright so we may have a couple hours or whatever but it'll be fine all right so we have a class called customer and then in a class you have two core components that's properties and methods properties are basically just attributes of the class all right so we have a customer class so we may have something like an ID let's say a name email ID name email and let's say location okay now when we're using PHP 5 or newer which most likely you are you want to you want to use what's called an access modifier all right also sometimes called an access identifier and there's three different ones you can use okay that's public which means that and I'm going to go over this more in depth in a little bit but public just means that you can access this this property from outside of this class alright we also have private okay that's going to make it so you can't access this anywhere except this class okay you can't access it outside of it you can access it from another class even if it's extended from this one all right and then you have protected which means that you can't access this from outside but you can access it from another class that extends customer alright so for example if we have let's say class my class extends customer okay if we do that then we'll be able to access email from in that class all right so hopefully that makes sense but what I'm going to do is I'm going to settle these the public for now and we'll get location all right so now we have some properties for our customer class actually now instead of location let's use balance okay since their customers they probably have some kind of balance now we're just declaring these declaring these but you can also set a default okay if we wanted to say ID equals one we could certainly do that okay and next thing is methods methods are functions so try not to get confused with methods and functions all a method is is a function that's inside of a class all right so you can also use access modifiers on that as well so we'll say public function and let's say I get tomorrow all right now this most likely would take in an ID okay so when you call this you would put the customer ID in and then most likely what you would do is you would take the ID that's passed in and you would assign it to the property ID so to do that what we would do is say this ID equals ID all right so if you ever want to access a property in your class you could use the keyword this and then you want that and then ID all right or whatever the property is all right that way you can access these from every method in your class all right because if we just keep it as ID and get customer we can't access this from anywhere else now a method like get customer what it would probably do in a real application is it would go and fetch a customer from the database with that particular ID all right and then you would probably just return the customer so we don't have the database part but what we can do is just say return and let's say it grabs a user called John Doe all right our customer called John Doe so now I'm going to show you how we can instantiate this class inside of our PHP alright so we want to make sure that we go outside of the class now all right now to instantiate it you're going to create a variable let's say customer and we want to set it to new and then the name of the class so new customer all right and that gives us a new customer object or instance all right and then we can continue and we can actually access public properties and and public methods or functions all right so for instance let's say that we want ID to be 1 by default then we can go and we can echo customer ID save it reload and we get 1 now just to demonstrate these the visibility or the access modifiers here let's make this private okay and if we go and reload we're going to get this cannot access private property okay if I change it to protected then it's saying cannot access protected property all right so let's make it back to public and get rid of that okay now if we want to call a method we can do the same thing we're going to echo customer and let's say get customer and then we need to pass in an ID all right so if we go ahead and reload we get John Doe because that's what we're returning here all right if we want to return the ID we could do that okay so let's pass in 11 and reload and we get 11 all right and then the same thing if I set this to private we get the error okay same thing with protected all right so that takes care of properties and methods now we also have a method called construct ok which is a constructor and there's a lot of different programming languages that use constructors what it is is it's a special function that or method that is going to run when we instantiate the object so when we run this if we have a construct function that's going to go ahead and run all right so let's create one we'll say public function and then you want to use double underscore and then construct okay just like that and just to test it out we'll say echo I will say the constructor brand alright so now let's save and all we're doing here is instantiating the customer object okay and you can see the constructor ran and we also have another one and these are called magic methods okay and there's a bunch of them I'm going to go over some more in a little bit but constructing in d-struct our magic methods all right so let's say public function struct and we'll do the same thing we'll just echo we'll say it will say destruct destructor ran all right so if we reload you can see we get the constructor ran and then the destructor ran all right and if we go ahead and spit out the customer again let's actually just return the name okay when we run this it should go in between the construct and destruct functions all right so let's say customer get customer and just put in 10 ok now if we reload oops we got to echo it ok so now you can see that John Doe is being printed out in between the two now common practice would be if we're going to just get a customer here and we're going to pass in an attribute or property and then assign it like this we can do that in the construct alright so it would take away the need to have this really all right so unless you're dealing with the database and all that you would do it but if we're just assigning class properties then you want to do that in the construct right so in here let's say ID name email and balance okay and then let's let's copy that and we'll just paste that here okay so we get ID then we'll do name this will be email and balance all right and I like to make these nice and neat okay and then we can get rid of this all together okay and we don't need this destruct either and we'll get rid of this all right so what we can do now is when we instantiate the customer we want to actually pass in the values here all right so the ID let's say one then the name will say my name and then email say Brad at Gmail and then a balance let's just say zero okay so if we want to output any of these we can now just say echo customer and let's echo out the name okay and that's going to give us the name so now we have this customer that we can work with if we wanted to create a function called let's say get email all right and then we can just return this email all right and then instead of doing this well let's get rid of that say we want the email and that gives us the email and notice we don't have to pass anything in here we don't have to pass in an ID or anything because we have our customer right here it's an instance it's just talking about this one customer all right so we can continue to run methods on that customer now the reason that you would want to do this is because you couldn't keep your properties all private and then you could just have a method that gets that private property okay so let's make all these private and this is really common practice okay so if we go ahead and save that reload we're not getting any error and it's letting us get the email address because we're not accessing it directly okay we're not saying customer email okay if we do that we're going to get that error okay because what we're doing is we're using this method here to get the private property all right and this is public I mean if I set this to let's say protected and reload we're going to get an error okay so public methods private properties is is really good practice so let's put that back all right so now let's talk a little bit about inheritance okay so as I said you can have a class extend another class so what I'll do is let's let's comment this out for now and we're going to create another class so let's say class and normally you would have these in their own files okay you'd have a customer for instance a customer dot PHP and then whatever else you have for classes in this case let's call it this subscriber okay so subscriber is going to extend customer so we want to say extends K make sure it's plural extends customer okay so now we have two classes now in subscriber let's create its own property okay so we'll say public and let's say there's a property called plan okay this is a subscriber and they're on a specific plan okay and plan is not part of customer because not all customers are subscribers now when we instantiate a subscriber we want to do the same thing that we did with customer it we want to pass these in alright but we don't want to repeat having to put all these down here and these so what we'll do is let's create our construct I'm going to copy this one alright now we're going to have to pass in all the stuff ID name email balance but we don't want to have to do this again alright so let's get rid of that and what we can do here is we can specify that we want the parent construct to run okay so do that I'm going to say parent and then a double colon and then construct okay just like that and then we want to pass these in alright now when we instantiate a subscriber we also want to put in the plan alright so we want to make sure we put that here but we don't want to pass it to the parent because there is no plan up here okay it's not um it's not a customer property it's a subscriber property so what we'll do is down here we'll say this plan is going to be equal to the plan that's passed in alright and that'll take care of that and now what we can do is let's take the get email and cut that out and paste it in here alright so now we have subscriber get email and we want to make sure that the email up here and the properties is set to protected okay because that means we won't be able to access it from outside unless it's a class that extends customer okay so now what we can do is copy that make sure you're outside of both classes and this is going to be now subscriber it's going to equal new subscriber and now we can actually pass in the plan as well okay so we'll say Pro alright and then we should be able to call get email so subscriber and we'll say get email actually we need to echo it out all right and now we can get our email okay through subscriber now if we wanted the get email to actually stay in the customer class we can do that because since subscriber extends customer we have access to this alright so we can actually save that and that's going to do the same thing all right now next thing we want to talk about is abstract classes alright and abstract methods now when you have an abstract class it can't be instantiated okay so if we make customer abstract we can't do this down here we have to just use classes that extend it alright so let's take a look at that so we'll say abstract class customer all right and just to show you what it does let's comment this stuff out and then we'll uncomment that and reload and you get cannot instantiate abstract class customer okay so let's comment that back out and then let's uncomment this and let's try to call get email all right so that's going to work but if we want to make let's I just want to show you if we take away the abstract in the class and add it here on to a method and then we try it's telling us that get email can't be run and the reason for that is when you have an abstract method like this it needs to be in an abstract class okay so we need to put that back here okay and then reload wait a minute here echo gets subscriber email cannot contain body oh that's because when we have our function here we can actually include a body all right so what we'll have to do is copy that and then this is we're just going to declare it like that all right and then down here is where we want to call it like that alright so you just want to define your functions in the abstract class okay it's basically you can think of it as a base class that that you don't use directly but you extend classes from it all right so hopefully that makes sense in my experience I haven't really needed to use abstract classes but it's it's it's good to know it's a fundamental part of object orientated program alright so this is starting to get a little cluttered so I'm going to just get rid of all this and what I want to move on to now is the static keyword all right so you can set static properties and methods you don't define the classes static but the properties and methods in side of it you can all right so let's create a class let's call it user and let's say user has just a regular property of username and also password okay now the purpose of using static classes and properties is that you don't have to create an instance you don't have to instantiate when you do that alright so let me just give you an example and we're going to say public static and let's say password length okay and we're going to set that to a default of 5 all right so password length would be a good thing to make static because you don't need a certain instance it's always going to be the same the user name and password are going to be different depending on the user but password length is going to be the same no matter what so that's a good example to use a static property all right and then we want to create let's say public public static function and we'll say get password length all right and what we want to do is return now when you're dealing with static properties you don't want to use this you don't want to do that what you want to use is the keywords self and then you want to use this double colon and then the property ok so in this case it's going to be password length okay so what we want to do now is let's go outside of the user outside of the user class and to call this we don't need to say you know user equals new user or anything like that what we can do is just the name of the class and actually let's echo out user and then same thing you want to use this double colon and then the function which is going to be get password length okay just like that let's go ahead and reload and it's giving us five all right and you would probably have this private but if it's public and you want to access it directly you can do that as well okay of course if I said private it's going to give us an error all right so that's how static static properties and static methods work all right so that's going to be it for now those are the very basic fundamentals that you need to know when working with object orientated pH all right I will hopefully create a part two of this where we can actually create an application that's going to that's going to use these concepts alright so for now thanks for watching please subscribe leave a like leave a dislike if you didn't like it and I will see you next time
Info
Channel: Traversy Media
Views: 123,686
Rating: undefined out of 5
Keywords: php, php oop, oop, oop php, php classes, php objects, abstract classes, static methods, static classes
Id: dQxuYRNbL_M
Channel Id: undefined
Length: 24min 5sec (1445 seconds)
Published: Sat Feb 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.