[0:00] hey guys this video is going to be on [0:03] object orientated PHP all right so if [0:07] you do not know any PHP at all I would [0:09] suggest that you at least learn the the [0:12] very Basics uh at least things like [0:15] variables arrays functions Loops just [0:18] just the the the real fundamentals of [0:22] PHP all right uh if you if you don't [0:25] know PHP but you have experience in [0:27] another language especially if it it's [0:29] if it's an object orientated language [0:32] then you should be all right all right [0:34] uh what what we're going to be doing is [0:35] going to be pretty simple so if you [0:37] already know uh object-orientated PHP [0:40] then you probably don't want to continue [0:42] because it's it's probably just stuff [0:44] you already know um but feel free to [0:46] watch and and maybe use it as a [0:48] refresher all right so what I have is I [0:51] already have my server set up I'm using [0:53] zamp or xamp and I have my local host at [0:58] local. and then I just have a index PHP [1:01] file so the first thing we're going to [1:03] go over is classes okay classes are the [1:08] basically the building block of [1:10] object-oriented programming at least in [1:12] PHP all right so to define a class you [1:15] just want to use the word class and then [1:17] whatever you want to call it in this [1:19] case I'm going to call it customer all [1:21] right and I haven't already planned out [1:23] this code just to let you know I'm just [1:25] kind of uh freestyling it all right so [1:28] we may have a couple errors or or [1:30] whatever but um it'll be fine all right [1:33] so we have a class called customer and [1:35] then in a class you have two core [1:37] components that's properties and methods [1:40] properties are basically just attributes [1:42] of the class all right so we have a [1:44] customer class so we may have something [1:47] like um an ID let's say uh a [1:55] name [1:58] email um I D name [2:01] email and let's [2:04] say [2:06] location okay now when we're using PHP 5 [2:11] or newer which most likely you are you [2:14] want to you want to use what's called an [2:16] axis modifier all right also sometimes [2:19] called an axis identifier and there's [2:21] three different ones you can use okay [2:23] there public which means that and I'm [2:26] going to go over this more in depth in a [2:28] little bit but public just means that [2:30] you can access this this property from [2:32] outside of this class all right we also [2:35] have private okay that's going to make [2:38] it so you can't access this anywhere [2:41] except this class okay you can't access [2:43] it outside of it you can't access it [2:46] from another class even if it's extended [2:48] from this one all right and then you [2:50] have [2:52] protected which means that you can't [2:55] access this from [2:56] outside but you can access it from [2:59] another class class that extends [3:01] customer all right so for example if we [3:03] have let's say [3:05] class um my [3:08] class [3:10] extends customer okay if we do that then [3:13] we'll be able to access email from in [3:15] that class all right so hopefully that [3:18] makes sense uh but what I'm going to do [3:20] is I'm going to set all these to public [3:22] for [3:27] now okay and we'll get location [3:33] all right so now we have some properties [3:34] for our customer [3:36] class actually you know what instead of [3:38] location let's use [3:40] balance okay since they're customers [3:42] they probably have some kind of [3:45] balance now we're just declaring these [3:48] declaring these but you can also set a [3:50] default okay if we wanted to say ID [3:52] equals 1 we could certainly do [3:54] that okay [3:56] and next thing is methods methods are [4:00] functions so try not to get confused [4:02] with methods and functions all a method [4:04] is is a function that's inside of a [4:06] class all right so you can also use uh [4:10] access modifiers on that as well so [4:12] we'll say public function and let's [4:16] say uh get [4:20] customer all right now this most likely [4:22] would take in an [4:25] ID okay so when you call this you would [4:27] you would put the customer ID in [4:31] and then most likely what you would do [4:32] is you would take the ID that's passed [4:34] in and you would assign it to the [4:37] property ID so to do that what we would [4:39] do is say [4:41] this ID equals ID all right so if you [4:45] ever want to access a property in your [4:47] class you could use the the keyword this [4:51] and then you want that and then ID all [4:54] right or whatever the property is all [4:56] right that way you can access these from [4:58] every method in your class [5:00] all right because if we just keep it as [5:02] ID and get customer we we can't access [5:05] this from anywhere [5:06] else now uh a method like get customer [5:09] what it would probably do in a real [5:12] application is it would go and fetch a [5:15] customer from the database with that [5:17] particular ID all right and then you [5:19] would probably just return the customer [5:21] so we don't have the database part but [5:23] what we can do is just say return and [5:26] let's say it grabs a user called John do [5:31] all right our customer called John [5:33] do so now I'm going to show you how we [5:36] can instantiate this class inside of our [5:39] PHP all right so we want to make sure [5:41] that we go outside of the class now all [5:43] right now to instantiate it you're going [5:45] to create a variable let's say customer [5:49] and we want to set it to New and then [5:52] the name of the class so new customer [5:55] all right and that gives us a new [5:57] customer object or instance all right [5:59] right and then we can continue and we [6:01] can actually access public properties [6:04] and and public methods or functions all [6:07] right so for instance uh let's say that [6:10] we want ID to be one by default then we [6:13] can go and we can [6:15] Echo customer [6:18] ID save it reload and we get one now [6:23] just to demonstrate these uh the [6:25] visibility or the axis modifiers here [6:28] let's make this [6:31] private okay and if we go and reload [6:34] we're going to get this cannot access [6:36] private property okay if I change it to [6:41] protected then it's saying cannot access [6:44] protected property all right so let's [6:47] make it back to public and get rid of [6:49] that okay now if we want to call a [6:51] method we can do the same thing we're [6:53] going to Echo customer and let's say get [6:57] customer and then we need to pass in an [6:59] ID [7:00] all right uh so if we go ahead and [7:03] reload we get John Doe because that's [7:05] what we're returning here all right if [7:07] we want to return the ID we could do [7:11] that okay so let's pass in [7:15] 11 and reload and we get 11 all right [7:20] and then the same thing if I set this to [7:24] private we get the error okay same thing [7:26] with [7:28] protected all right so that takes care [7:31] of properties and methods now we also [7:34] have a method called construct okay [7:37] which is a Constructor and there's a lot [7:40] of different programming languages that [7:41] use Constructors what it is is it's a [7:44] special function that or method that is [7:47] going to run when we instantiate the [7:50] object so when we run this if we have a [7:53] construct function that's going to go [7:55] ahead and run all right so let's create [7:57] one we'll say public function and then [8:00] you want to use double underscore and [8:03] then [8:06] construct okay just like that and just [8:09] to test it out we'll say [8:12] Echo uh we'll say [8:15] the [8:17] Constructor [8:20] ran all right so now let's save and all [8:23] we're doing here is instantiating the [8:25] customer object okay and you can see the [8:28] Constructor ran [8:30] and we also have another one and these [8:32] are called Magic methods okay and [8:33] there's a bunch of them I'm going to go [8:35] over some more in a little bit but [8:37] construct and and um destruct are magic [8:41] methods all right so let's say public [8:47] function [8:50] destruct and uh we'll do the same thing [8:53] we'll just [8:54] Echo we'll say we'll say destruct [9:00] uh Destructor [9:04] ran all right so if we reload you can [9:08] see we get the Constructor ran and then [9:09] the destructor ran all right and if we [9:13] go ahead and spit out the customer again [9:15] let's actually just return the [9:20] name okay when we run this it should go [9:23] in between the construct and destruct [9:26] functions all right so uh let's say [9:29] custom [9:30] customer [9:32] get customer and just put in 10 okay now [9:36] if we [9:37] reload uh oops we got to Echo [9:42] it okay so now you can see that John Doe [9:45] is being printed out in between the [9:47] two now common practice would be if [9:50] we're going to just get a customer here [9:53] and we're going to pass in um an [9:55] attribute or property and then assign it [9:58] like this we can do that in the [10:00] construct all right so it would take [10:02] away the need to have this really all [10:05] right so um unless you're dealing with [10:08] the database and all that you would do [10:10] it but if we're just assigning uh class [10:13] properties then you want to do that in [10:15] the construct all right so in here let's [10:17] say [10:18] ID uh [10:21] name [10:25] email and balance okay and then Let's uh [10:29] let's copy that and we'll just paste [10:33] that [10:34] here okay so we get ID then we'll do [10:40] name this will be [10:47] email and [10:53] balance all right and I like to make [10:57] these nice and neat [11:02] okay and then we can get rid of this all [11:05] together okay and we don't need this [11:07] destruct [11:09] either and we'll get rid of this all [11:12] right so what we can do now is when we [11:15] instantiate the customer we want to [11:17] actually pass in the values here all [11:19] right so the ID let's say [11:22] one then the name we'll say my [11:27] name and then an email Ma say Brad at [11:33] Gmail and then a balance let's just say [11:37] zero okay so if we want to Output any of [11:41] these we can now just say [11:44] Echo [11:46] customer and let's Echo out the [11:50] name okay and that's going to give us [11:53] the name so now we have this customer [11:55] that we can work with if we wanted to [11:58] create a function [11:59] [Music] [12:01] function called let's say um get [12:07] email all right and then we can [12:10] just [12:11] [Music] [12:12] return [12:14] this email all right and then instead of [12:18] doing this well let's get rid of that [12:21] and say we want the [12:25] email and that gives us the email and [12:27] notice we don't have to pass anything in [12:29] here we don't have to pass in an ID or [12:31] anything because we have our customer [12:33] right here it's an instance it's just [12:35] talking about this one customer all [12:38] right so we can continue to uh run [12:41] methods on that [12:43] customer now the reason that you would [12:45] want to do this is because uh you could [12:49] keep your properties all private and [12:51] then you could just have a method that [12:53] gets that private property okay so let's [12:56] make all these [12:57] private and this is really common [13:04] practice okay so if we go ahead and save [13:07] that reload we're not getting any error [13:10] and it's letting us get the email [13:11] address because we're not accessing it [13:13] directly okay we're not saying customer [13:16] email okay if we do [13:20] that we're going to get that error okay [13:23] because what we're doing is we're using [13:24] this method here to get the private [13:27] property all right and and this is [13:29] public I mean if I set this to let's say [13:33] protected and reload we're going to get [13:35] an error okay so public methods private [13:39] properties is uh is really good practice [13:43] so let's put that back all right so now [13:47] let's talk a little bit about [13:49] inheritance okay so as I said you can [13:52] have a class extend another class so [13:55] what I'll do is let's um [14:00] let's comment this out for now and we're [14:03] going to create another [14:05] class [14:07] um so let's say class and normally you [14:11] would have these in their own files okay [14:13] you'd have a customer for instance a [14:15] customer. PHP and then um whatever else [14:19] you have for classes in this case let's [14:21] call it this uh [14:25] subscriber okay so subscriber is going [14:29] to extend customer so we want to say [14:32] extends okay make sure it's plural [14:35] extends [14:38] customer okay so now we have two classes [14:42] now in [14:43] subscriber let's create its own property [14:47] okay so we'll say [14:49] public and let's say there's a property [14:52] called plan okay this is a subscriber [14:55] and they're on a specific plan okay and [14:58] plan is not part of Customer because not [15:00] all customers are [15:02] subscribers now when we instantiate a [15:05] subscriber we want to do the same thing [15:07] that we we did with customer it we want [15:09] to pass these in all right but we don't [15:11] want to [15:12] repeat um having to put all these down [15:16] here and these um so what we'll do is [15:19] let's create our construct I'm going to [15:22] copy this [15:23] one all right now we're going to have to [15:26] pass in all the stuff ID name email [15:28] balance [15:29] but we don't want to have to do this [15:31] again all right so let's get rid of that [15:33] and what we can do here is we can [15:36] specify that we want the parent [15:37] construct to run okay so to do that [15:40] we're going to say parent and then a [15:41] double colon and [15:44] then [15:46] construct okay just like that and then [15:48] we want to pass these [15:50] in all right now when we instantiate a [15:53] subscriber we also want to put in the [15:55] plan all right so we want to make sure [15:57] we put that here but we don't want to [15:59] pass it to the parent because there is [16:01] no plan up here okay it's not a um it's [16:05] not a customer property it's a [16:06] subscriber property so what we'll do is [16:09] down here we'll say this plan is going [16:12] to be equal to the plan that's passed in [16:14] all right and that'll take care of that [16:17] and now what we can do [16:19] is let's take the get [16:23] email and cut that [16:26] out and we'll paste it in here [16:29] here all right so now we have subscriber [16:32] get email and we want to make sure that [16:35] the email up here in the properties is [16:37] set [16:38] to protected okay because that means we [16:41] won't be able to access it from outside [16:43] unless it's it's a class that extends [16:46] customer okay so now what we can do is [16:49] copy that make sure you're outside of [16:52] both classes and this is going to be now [16:57] subscriber he's going to equal new [17:01] subscriber and now we can actually pass [17:04] in the plan as well okay so we'll say [17:08] Pro all right and then we should be able [17:10] to call get [17:13] email so [17:17] subscriber and we'll say get [17:22] email actually we need to Echo it [17:27] out all right and now now we can get our [17:30] email okay through [17:33] subscriber now if we wanted the get [17:35] email to actually stay in the customer [17:40] class we can do that because since [17:44] subscriber extends customer we have [17:46] access to this all right so we can [17:48] actually save that and that's going to [17:51] do the same thing all right now next [17:54] thing I want to talk about is abstract [17:57] classes all right and abstract methods [18:00] now when you have an abstract class it [18:02] can't be instantiated okay so if we make [18:05] customer abstract we can't do this down [18:08] here we have to just use classes that [18:10] extend it all right so let's take a look [18:13] at that so we'll [18:16] say abstract class customer all right [18:19] and just to show you what it does let's [18:23] comment this stuff out and then we'll [18:24] uncomment that and reload and you get [18:28] cannot instantiate abstract class [18:31] customer [18:33] okay so let's comment that back [18:37] out and then let's uncomment this and [18:41] let's try to call get [18:45] email all right [18:48] so that's going to work but if we want [18:50] to make let's I just want to show you if [18:53] we take away the abstract in the class [18:55] and add it [18:57] here onto a method and then we try it's [19:01] telling us that get [19:03] email uh can't be run and the reason for [19:07] that is when you have an abstract method [19:09] like this it needs to be in an abstract [19:11] class okay so we need to put that back [19:17] here okay and then [19:20] reload uh wait a minute [19:24] here Echo get subscriber [19:27] email cannot contain [19:32] body um [19:34] oh that's because when we have our [19:37] function here we can't actually um [19:40] include a body all right so what we'll [19:41] have to do is copy that and then this is [19:45] we're just going to declare it like that [19:47] all right and then down here is where we [19:50] want [19:51] to call it like [19:55] that all right so you just want to [19:57] Define your fun functions in the [19:59] abstract class okay it's basically you [20:02] can think of it as a base class that [20:04] that you don't use directly but you [20:07] extend uh classes from [20:10] it all right so hopefully that makes [20:13] sense um in my experience I haven't [20:16] really needed to use abstract classes [20:19] but um it's it's it's good to know it's [20:21] a fundamental part of um object-oriented [20:25] program all right so this is starting to [20:27] get a little cluttered so I'm going to [20:29] just get rid of all [20:31] this and what I want to move on to now [20:35] is the static keyword all right so you [20:38] can set static properties and methods [20:42] there you don't Define the class as [20:44] static but the properties and methods [20:46] inside of it you can all right so let's [20:48] create a [20:50] class [20:51] um let's call it [20:55] user and let's say user has just a [20:59] regular uh property of [21:04] username and [21:07] also [21:09] password okay now the purpose of using [21:12] static classes and properties is that [21:15] you don't have to create an instance you [21:18] don't have to [21:19] instantiate uh when you do that all [21:21] right so let me just give you an example [21:24] and we're going to say public static [21:28] and let's say password [21:31] length Okay and we're going to set that [21:34] to a default of five all right so [21:37] password length would be a good thing to [21:39] to make static because you don't need a [21:43] certain instance it's always going to be [21:44] the same the username and password are [21:47] going to be different depending on the [21:49] user um but password length is going to [21:51] be the same no matter what so that's a [21:53] good example to use a static property [21:57] all right and then we want to create [21:58] let's say [22:00] public um public [22:04] static function and we'll say [22:08] get password [22:11] length all right and what we want to do [22:16] is return now when you're dealing with [22:19] static properties you you don't want to [22:20] use this you don't want to do that what [22:23] you want to use is the keyword self and [22:26] then you want to use this double colon [22:28] and and then the property okay so in [22:30] this case it's going to be password [22:37] length Okay so what we want to do now is [22:40] let's go outside of the user outside of [22:44] the user class and to call this we don't [22:47] need to say um you know user equals new [22:52] user or anything like that what we can [22:54] do is just the name of the class and [22:57] actually let's Echo out [23:00] user and then same thing you want to use [23:02] this double colon and then the function [23:04] which is going to be [23:06] get password [23:09] length okay just like that let's go [23:12] ahead and reload and it's giving us [23:15] five all right and you would probably [23:18] have this private but if it's public and [23:19] you want to access it [23:21] directly you can do that as [23:24] well okay of course if I said private [23:31] it's going to give us an error all right [23:33] so that's how static uh static [23:35] properties and static methods work all [23:38] right so that's going to be it for now [23:39] those are the the the very basic [23:41] fundamentals that you need to know when [23:44] working with object orientated PHP all [23:47] right I will hopefully create a part two [23:50] of this where we can actually create an [23:52] application that's going to that's going [23:54] to use um these Concepts all right so [23:57] for now thank thanks for watching please [23:59] subscribe leave a like leave a dislike [24:01] if you didn't like it and I will see you [24:03] next time