PHP OOP Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the first episode in the PHP object orientated programming series today we're going to focus on sort of a brief overview if you will of ropey in PHP I'm going to demonstrate how we can actually define a class as well as an explanation as to what a class is what is the difference between classes and objects and how we can look at everyday objects as classes and create those in our programming world so what is a class and what is an object well a car versus but a class is basically a definition of an object a class defines what the object is in the sense that what the properties are and and the actions that the object can have and an object is it's basically an instance of a class it's a member it's a it has a state a series of properties and values that you just you define explicitly so for example you define say the first name or the last name whereas the class actually defines that there is a first theme and there is a a last name but it doesn't actually define what they are so you can think of an object as an instance of a class okay so you can have lots of objects you can have lots of classes as well but you can have lots of objects that that are objects of or instances of classes right so if you were to think of a vehicle or a car for example and you had a car class that car could have a series of properties such as number of doors color of the car size of engine that kind of thing whereas the object would define the actual the actual values so for example then the the actual value of the number of doors so you could have four doors you could have five doors you have three doors number of you know the engine size and so forth and the color blue or white or black whereas the class knows that there is a property called color but it doesn't actually know the value of that of that of that property it's basically a blueprint a manifest of what the object can contain it's also a blueprint of how the class acts both within itself so a series of actions a series of methods that the car can do it doesn't necessarily define how those methods work because you can actually inject properties into those methods and so forth but for example with a car you have a map you could have a method of starting and stopping and turning and pressing the indicators on and so forth but the object could define how fast the car is going or you know how loud the music is playing and so forth because you can actually put in you know properties into those methods so you could have a method of off of turn the radio on and then you could supply a series of properties to define the volume in which the the radio sound you know the audio is playing at another example could be people okay so you could have a class that is a person and you could have a series of properties that the class defines so for example hair color gender first name and last name and then the object the member the instance of that class will define will actually define the value of the first name the value of the last name whether it's a person is a female or agender or whatever and that say the higher hair color and so forth and likewise with the car a person can also have methods for sleeping and waking and working and eating and so forth and also talking so greeting and so forth let's just let's just create some classes now it might come out a little bit more obvious as I do that so I'll stop my rambling and get on with the code so the first thing we're gonna do in PHP is actually define the class by the keyword class and we're going to use the person example as I mentioned before so we're going to we're going to add the name of the class here which is person and we're going to open up the curly braces and close the curly braces like so okay now the next thing we're going to do is define properties that make up this class and the properties are first name like so as well as last name and we're also going to add gender as well now you may have noticed that this red exclamation mark here that my IDE has picked up and that's because it's there is an unexpected variable of first-name this is basically because I haven't declared yet the type the data the type of property that I would like to have and there's three types of properties that we can choose from the first is public second is protected and thirdly and lastly it's private now those types change the behavior of the properties so as a quick rundown public allows you to access the property outside of the class as well as change its definition so you change its value for example protected only allows the property to be accessed from within the class as well as from within an extending class now I'll talk about class extending classes and so forth in the future tutorials and private basically means that it's only accessible internally to that class it can't be accessed by extending classes and nor can it be accessed from outside of the class again don't worry too much about those three I will demonstrate in more detail as I as I go on right now what I'm going to do is just define these as public so public first name public last name oops and public gender like so excuse me now what we need to do is instantiate the class and what there's a magic method that gets caught during instantiation and that is the constructor method and like the properties we need to define the type of method and all methods within the class can have these types so again it's pipe private public and protected they have a similar meaning as the properties in the sense that private can only be accessed internally to that class protected can be overwritten and accessed in other classes and public can be accessed from outside of the class don't worry too much again about those just yet we'll get into those in future tutorials but right now I need to create a public oops a public function which is the oops the constructor now the constructor is a magic method as I mentioned magic methods start with underscore underscore and here we go so let's just save that just so we can format it properly and we're gonna actually supply some properties into this into this constructor the first property is the first name and the second property is last name and you guessed it the last property will be the gender but I'm going to eat call that 2f so we're going to default that to F for female now what we need to do is set the values on these properties and we do so like this so we put in a keyword called this and then - greater than symbol and then first name now notice my IDE IDE has picked up that we have these three options here and the first name last name because it knows that the scope of this has access to these three properties so again oops I should do that in between just for consistency this last name is equal to last name this first name is equal to first name gender is also equal to gender like so and we're gonna save that now the thing that we need to do in order to create this person class is we need to instantiate it we need to create an object a member instance that will have some states some explicit values set against this person so we're just going to create a person called Tom and we're going to equal that to a new person now what that does is it instantiates the person class and we can put in three properties in here Jen first name last name and gender so the first name here I'm just going to have Tom last name I'm gonna have been and whoops and I'm not gonna bother with the gender as Valley is optional which will be set to actually no I will I'll set that to M for male ok let's save that and then what I'm gonna do is echo out Tom I'm gonna echo out the first-name like so and then I'm going to echo out a new line like that I'm also going to open up the terminal so I can run the file so PHP tutorial 1 we echo that and we have Tom so we have we've accessed the first name first name is the property on here now a couple of things that we can do is we can actually create a method to pull out of the first and last name and we can do this like so it's gonna be a public method because we're accessing it outside of the class and it's gonna be a function and we're gonna call this function say hello this is going to be an action that this person can do so we are going to return a string which says hello my name is and we're gonna access the properties we're gonna concatenate these properties so this first name and if you followed along with my previous tutorials I have a demonstration of concatenation and more information as to how we can concatenate variables but for now we're just going to concatenate this string so first name space and then this last name like so and we're gonna save that and then what we can do here on the person is instead of call the first thing directly we can call this method so say hello like so and we're gonna save that and then run this okay so now we have hello my name is Tom Ben and excuse me and that we can create several objects several member objects of this class there's several class instances so we can have Tom and we can have Jane and Jane is going to equal a new person and we're going to instantiate person with Jane as the first name and the last name is I'm just gonna keep Ben for example save that and we're also going to do a new line here we're going to do Jean Jane say hello as well as Tom say hello and we're gonna put in the new line just give us a ring and then run that so now we have this we have hello my name is Tom Ben hello my name is Jane Ben so can you see that we are basically encapsulating this information within these two objects we've created a single class called person and we are instantiating the person with some properties and the properties get set during the constructor constructor gets called on instantiation so we call that automatically passing in the first name last name and agenda like so now other things that we can do is we can we can also return the gender as well so for example we can do public function get gender and we can just return this gender just like we've done before with the first name and last name and so we can do say hello then we're going to concat that to gender is equal or colon to Jane get gender like so and we're gonna save that run this and we should see that we have an F here okay so that's a brief a very very brief explanation into classes and objects I will be going into further far more detail as the tutorials progress we're going to talk about public protected and private properties and methods in more detail in the next tutorial next Tuesday so do subscribe to get that but if you've gotten any comments so far then please or questions then please leave them in the comment section below or you can grab hold of me via Twitter my Twitter handle is at P FWD like the video if you found it helpful and do subscribe to get the next PHP tutorial next Tuesday as well as the web chat this Friday thank you ever so much for watching and I shall see you again soon thank you bye hello and welcome to the second part of the object orientated programming course in PHP today we're going to look at class inheritance or a very basic tutorial in classical inheritance in the last tutorial we did we focused on actually creating classes and objects in PHP and the differences between classes and objects as well so do check out that video if you haven't done so already I'll leave a link in the description as well as in the top right hand corner here but today we're going to focus on inheritance and and Clark Meno class inheritance and how one class can inherit the properties and the methods that we allow in another class now why would you want to do that why would you want to have inheritance well there could be objects that need to have access to other properties other methods that the other objects may offer and they may be closely related so for example let's let's use an example of an employee right so right now we have a person with the first-name lastname enjoy and gender we could also have a class of employee so for example if we scroll down here we could have you know Jane here that we've instantiated a new person class she could be an employee somewhere and so the employee of her class would have properties that are that should be encapsulated for the employee class so for example think she may have a job title and an employee number and so forth but an employee is also a person and therefore you could have the employee class extending a personal class and that will allow the employee class to have access to certain properties and methods that we are we make available so in this case because everything's set to public and I will get on to visibility scopes further down the line in these tutorials but we can have we can access the first name last name and gender as well as accessing these these methods here so let's do that let's create an employee class that extends the person class and I'll demonstrate how we can have access to these these are these properties and these methods yeah excuse me I've got a bit of a cough so okay let's let's build the class so right now I'm going to just create the class as is and then I'm going to demonstrate inheritance once the class that's being built so we're kind of doing a new class and we're gonna call this employee and we are going to just give a single property which is just public and wait for now we're just going to do first not first name sorry job title like so and we're also going to create a constructor so public function constructor construct like so we're gonna pass in the job title as the first argument and we're going to do this job title is equal to job title like so and then we're just going to also have a public function get job and we're gonna return whoops like that okay so we've just created our our employee class we have a property of job title and a constructor which you pass in job title as the first argument and then that gets set so this job title is equal to job title here and we've also got a public function job title like I said I'm going to get into visibility scopes further down the line and and discuss what the difference is between these different scopes so the moment they're all public but let's let's not worry about that too much at the moment so let's scroll down here let's remove some of this stuff that we created in the last tutorial and let's just do employee is equal to a new instance so we're going to instantiate whoops sorry new employee coding whilst I'm talking so a new employee we're passing in a job title and this is just going to be back-end developer and what we also going to do is echo employee get job title and we're gonna save that let's just double check the values correct so we are actually echoing this out that's fine okay so now we should just see the back-end developer being printed out let's save that so PHP excellent okay so we have the back-end developer being printed that's fine okay great so at the moment the employee has no no concept or no snow association to the person so in order to inherit the person in order to extend the person we need to put in the keyword extends and then a space and then the class that we wish to extend so person like so now what have we just done well we've we've extended the person or sort of sorry we extended the employee to the person this main means that this is now the base class person and this is the sub class employee employees now hang off of person so now we should have the the scope of accessing the methods that are declared as public in this case of the of the class person but we're going to get a couple of issues because we haven't actually set the first name or the last name of the gender now notice that we have a constructor in here a public functioning constructor we're passing in the first-name lastname on the gender and in this constructor we're only passing in the job title now because this this this class inherits the person class we can now access the the public constructor here we can the person constructor and what we do is we use the parent keyword so I'm going to just put in the space under here we can actually do parent like so colon colon and then construct and we can access we can now set the the variables in the person constructor within this constructor here and now obviously I need to actually create or past these first name last names and genders in so let's just do that now so we can do first name last name gender now with setting gender to be what are we setting gender as default F so gender is equal to F let's just keep that consistent so let's save that excuse me so what we can do is scroll down here and change that to be Jane again so Jane is now an instance of employee we're just gonna change that to be Jane like so and we're actually now we need to give some more properties so we've got the back-end developer as the job title we also have the first name which is Jane we have the second name which we're gonna quote I don't know J I'm just gonna do Fisher for not for instance and then the the gender which we don't actually need because that gets set by default so let's just save that and click on to here so we have the job title the first name the last name the gender and we're then calling the constructor passing in these three variables so let's save that and it's just been sure that this still works so this should just return this just for my benefit just return the back end developer that's great now what's what what we've done here is we've instantiated the employee class we've passed in the the job title as the first argument and then these two properties they actually get set on the part person class that this employee class inherits so I can now do echo let's just put in a new line echo and then Jane get gender so let's save that okay and then run this let's just clear that down just some raw room okay so we now have the gender of female now or F so f gets set if that doesn't if by default so what we're doing is we're calling the constructor this this employee constructor which calls the the person constructor which is whoops let's just bring that back into focus there we go so it calls the person constructor there we go cause the person constructor passing in the first name and the last name the gender gender hasn't been passed and therefore it's set to F and then we're calling the get gender but we're calling the get gender from this employee class the reason why we can do that it's because we even extended the person class and therefore we have access to this method likewise if I was to do public sorry public public function get name for instance well actually what we can do sorry forgive me is we can just do say hello right so we can actually access this say hello method from this employee class and let's just do that there we go hello my name is Jane Fisher okay so what we can't do however is we can't do this we can't say we can't instantiate a new person so person we can't as to supply that we can't do person get job title if we were to save that and run this we will get an error and the error is is an undefined method person get job title because what we're doing here is we're in stana shading the person class which is this class here which has no knowledge of the employee okay so it's it's hierarchical right so we have to instantiate the employee class to be aware of the person variables or we can we can instantiate the person class but we'll have no knowledge of the employee okay so let's just bring that back to how it was and it's just um change that back to employee and save that and then run this again let's clear that down and run that again okay so that's a very very basic tutorial on class inheritance in PHP but if you've got any questions then please let let me know put them in the comment section below if you've found this tutorial helpful then give it a thumbs up press that like button if you think other people might find it helpful then please do share it around subscribe to get the next tutorials for the next couple of PHP tutorials as well as there's a Friday web chat every time every time every Friday I do a web chat is well just talking about web development in general so do subscribe to check that out thanks again for watching and I will see you again soon thank you bye hello and welcome to the third part in the PHP object orientated programming tutorials today we're going to look at visibility scopes if there's something that I've been banging on about in the last couple of videos where I've been saying I'll talk about this later on well today is the day this tutorial we're going to focus on visibility scopes and now there are three visibility scopes there's public private and protected now these visibility scopes can go on the methods as well as the properties so where you see here public public public all of these things these are visibility scopes so we have a public job title in this employee class a public constructor and a public get job title and likewise if we went down to the person class these are all public and these are all public too now I talked about class inheritance in the second tutorial in the previous tutorial do check that out if you haven't done so already as well as the first tutorial where I talked about classes and objects in general I'll leave a link to that in the description below for both of those as well as in the annotation bit up here in the card bit up here in the top right hand corner okay so let's talk about these scopes then so let's just let's just comment these just to talk about them so public private and protected now what do these do well the public one as you can probably guess allows public access to these properties and needs variables so for example let's scroll all the way down here where we're doing Jane the instant the instance of the employee click class get a job title we don't actually need to do that because if I was to just jump to that function it's returning the job title and the job title itself is public so we don't actually need to do call the get job title in order to return the property because we can access the property directly I'll just demonstrate that now let's scroll to the bottom again instead of calling get job title we are literally going to do Jane job title and we're going to save that out I'm just going to comment this just so we can focus on on this bit for now let's run that example so now we've actually returned the back end developer which is exactly exactly what the get job title method was doing but because the property is public which is public here it means that we can just access it directly not only that not only can we access it directly but we can also alter it we can change the value of this property outside of the scope of this class so let's scroll to the bottom again let's do Jane job title and we're gonna equal that to front-end developer and save that now this is actually going to change from because we what we're doing here is we're gain is a new instance of employee we're passing in the back-end developer as the first argument which is the job title our best just explain that just in case you haven't seen the other tutorials so in the constructor here we have job title and we are setting the job title to be the job title the first argument here okay so when we instantiate this class and we pass in the first argument the first argument being job title we're saving that to be job title like like that so let's just scroll down to the bottom so what we're doing here is we're actually on line 47 we're actually changing we're altering the value of job title to something else so in this case front-end developer and therefore on line 48 we're echoing out Jane job title this will actually change from back-end developer to front-end developer let's just save that down and run this whoops so there we go so we've actually changed it we've actually altered the value of this property see within this class outside of the scope of the class so on live 48 or sorry line 47 we are changing it from back-end developer to front-end developer like so so that's the public scope public basically means that you can access these properties and these these methods from outside of the scope of the class or you know from outside the scope of the class and we can also alter the values of the property as well now the polar opposite of public is private which is this one here so let's just change that to be private like so and save that scroll to the bottom and this is going to throw us an error because it's no longer public it's now private let's just clear this down and run the file and the error that we get is this here it's a fatal error it cannot access the private property employee job title so um so as you've probably guessed private properties are encapsulated within the object within the class itself so we cannot access job title from outside of the employee class job title must only be accessed from within the class so for example in this constructor here we're setting job title as the first argument and then we're accessing job title from within the scope of the employee class and we're setting it to the variable okay so let's scroll down here let's remove this setting and the way we would change actually at least this talked about yeah so the way we would actually change the value front-end developer from outside of the scope of the class is we would use what's called accessor methods now these are broken up into two different methods so we have we have sorry we have a getter and we have a setter so let's just build those in now so we have public function whoops set job title save that and we're gonna pass in the value of job oops job title and we are going to set this job title is equal to job title and save that now resources now because this is public it means that we can access this method from within the employee class and that is accessing the job title property even though it's private it's accessing it from within this class and therefore this can happen this can work we're setting that to job title so let's scroll to the bottom and instead of having instead of accessing the the property directly we're now going to call set job title and we're going to pass this in as the first argument so let's just copy that and save this okay I'm gonna run this again and it's still gonna throw an error I'll explain why in a minute let's just clear that down and run this again and the error again is it cannot access the private property employee job title but this time is on line 52 and that's because we're still trying to access it from outside of the scope of this class so echo Jane job title so what we need to do here is use the the second accessor method in this case it would be a getter so let's scroll to the top I think we already have one so yes so here we're doing a public function get job title and we're returning the job title so we can actually do that now so let's bring those back to life and then do stead of say hello we doing get job title whoops my best remove that there we go and there's no need for that too so let's save that clear the screen and and run this so now we can actually see that we've got front-end developer so we've we've changed it we've altered it using a public access a method which is a setter so we've set the job title passing in this value which it gets set to and then we're echoing out the get accessor method so get excuse me get job title excuse me now why would you want to do this what's the whole point in having getters and setters and so forth and and and exposing properties in this manner well we might want to force logic to go through setters if for example we want to enforce some sort of formatting rules and and therefore prevent these values from being changed from outside of the scope of the class unless they go through these setters which then enforce the formatting rules so let's just go back into this method here we might want to for example make sure that we have at first the uppercase the first letter within the job cycle and let's just run that save that and do change that to be a lowercase F for example let's save and then let's run this so even though we're trying to set it with a lowercase front end we've actually uppercase tit here and that's being enforced within this set of job title method like so and just for completeness what we can do here because we can also do this set job title passing in the job title like so and therefore anything that gets added to the constructor will also be uppercase and let's just demonstrate that now so let's just comment out this setter and let's just change that to be a lowercase B save that and run this whoops and there we go we've got a uppercase B for back-end developer even though we've put in a lowercase like so okay so that's private properties we can also have private methods so we could do we could have this as private so keep that private that means that we can only access this this method from within this class and let's scroll to the bottom here now we try to access this method from outside of the scope of this class so we're going to save that and we're going to just clear this we will now get an error and the error is that we can't call a private method get job title from the context of nothing there basically this means from the context of of outside of the scope of these classes now why would you want to do this well let's just scroll up and change that to public again because the employee class extends the person class now that this is extension and that inheritance is something that I touched on in the previous tutorial so do check that out if you haven't done so already but it basically means that we are ensuring that these methods can only be accessed from the scope of this class now if we were to extend it so for example here we're extending employee to person we can for example limit the scope so let's say we set that to private and save that we now no longer have access to this say hello method from the employee so let's try and do that so let's change that to be say hello and save that let's just ensure that that is correct so we are trying to run say hello from the person class that we are inheriting so employee class extends person so we have access to these public methods but we don't have access to this private method so let's just run that and we should have a an error which we do here so err a fatal error call to private method person say hello from context blank however if we were to change that to public for example and then clear the screen like so and then run that we now have access to this method so hello my name is Jane Fisher and so forth so by having public methods and private properties and private methods and so forth where we can actually create some rules in our architecture of our objects to ensure that the scope is correct for when we need it and for when we don't need it for example we wouldn't want to have everything just publicly accessible and changeable from outside of the class we might want to ensure that these variables get set these properties get set using some sort of formatting to perhaps clean the variables maybe there are user inputs and so forth and we need to sanitize them therefore they need to go through some some functions some some mechanisms to clean the variables out to trim them down to and to ensure that they don't have any SQL injection or anything like that and so forth and therefore we can encapsulate we can isolate the logic into these objects which is great for designing the architecture of the of the objects in the classes now the third one that we haven't yet focused on is protected now this is kind of a mishmash between public and private in the sense that it's protected and therefore we have access from from from the inheritance point of view but we don't have access from outside of the scope of the of the class let me just demonstrate that now so let's say we have we change this to be protected for example right so first name is protected let's just save that let's run this go through here and do Jane so we have access to the last names we can see in the IDE here we have last name and we also have gender but we no longer have first-name I'm just going to try and access that first name let's echo that out so echo first name and let's just comment you remove that we don't need that at the moment and save that clear this down and run the file the error we have here is again we cannot access a protected property employee first-name however we can access this from from the inheriting class so we can do for example we can do this first-name we can actually access the first name from the employee class but we can't access it from outside the scope of of the classes so from here for example and therefore we're ensuring that this first name property can only be accessed from this person class in any class that it inherits whereas private was basically saying that it can't like this this property put a private property cannot be accessed from any inheriting classes let's just demonstrate that now so let's just change that back to private and scroll up I now my IDE no longer gives me access to this first name it's only last name job title and gender whereas if I was to remove that change that to be protected save that scroll up my ID should now offer me access to the first name you see and so likewise what we want to do here is perhaps have a public method public function and we're gonna have let's just put the say hello method within here so let's just remove say hello from you or just copy that and put that in here so now the employee can say hello hello my name is first-name lastname and also let's just do a new line and also forgive me I'm thinking as I'm speaking so let's just put in message message is equal to hello my name is first-name lastname and also can cap that to be message my job title is and this job title like so let's let's just return the message okay so at the moment job title is set to private and we are accessing it from this class so that's fine first thing when lastname are are we gonna just set that to both be public like so so we should have access to all of these variables and let's scroll down to the bottom and let's do echo Jane say hello and we're just going to double check but yeah that is correct good and let's just clear this down and do and run that again so now we've got hello my name is Jane Fisher my job title is back-end developer let's just remove that blank space that back just to give us a little bit more formatting now if I was to change this to be private we'll get an error because we can't access the first name from here so let's just clear this run that again and we can see that we have an error so instead of so we got hello my name is and then we can't nothing there Fisher and this is actually a notice undefined property first name employee first name and that's because we can't have access to this here if I were to change that back to public and save that clear this down that will work like so now if I were to change that to protected and safe that it will also work let's just clear that down and then do that so now we have hello my name is Jane Fisher we've changed that to be protected the first name but what we don't have access to is the first thing from outside of the scope of this class let's just change that to be Jane first name oops if I can spell it the first name like so if I was to clear that down run that we now get this this cannot access protected property even though we can have access to this protected property from inside the employee class we don't have access to it from outside these classes this person and these employee classes either okay so that's private protected and public methods and properties in in the visibility scope if you've got any questions then please let me know put them in the comments section below if you found this video helpful and give it a thumbs up give it a like and share it around if you think others might find this useful too please subscribe to pick up the next tutorials I'm doing some more PHP tutorials as well as web chats every Friday so these are talks about web development every Friday so do check those out thanks again for watching follow me on twitter twitter handle is at p FWD i shall see you again next week thank you bye hello and welcome to the next part of the PHP object-orientated tutorials today we're going to look at property overloading using different magic methods that we haven't done so before in the previous tutorial we looked at visibility scopes in objects we were playing with the public private and protected properties as well as methods so if you haven't seen those already that tutorial already i've got a link in the description below and also in the top right-hand corner here do check that out because this tutorial will make more sense if you've if you've seen that okay so property overloading then we're gonna use some magic methods and the magic methods that we're going to use underscore underscore get an underscore underscore set now you may have noticed we've actually used some magic methods before we've used the constructor the construct here and the magic methods are basically start with underscore underscore these are reserved method names that PHP has so we couldn't create our own construct method with the underscore underscore because these have special meaning so for example this constructor here this construct gets called whenever the class is instantiated and likewise the set and the get magic methods are called whenever the property which cannot be accessed outside of the scope is accessed and set so for example we could have lots and lots of properties in employer-employee as well as in the person I suppose the class that has that are set to private so we could have lots of private properties and if we wanted to access them outside of the scope of the class we would need to use setters and getters as I've mentioned in a previous tutorials but if we have lots and lots and lots of these things then we would have lots and lots of methods to get and set so for example here in the person class where we have first-name lastname agenda if these were all set to private for example we would need to use getters and setters to access each one of these outside the script for class so we've got three properties here that would mean six methods so three getters and three setters let me just put those back to what they were so there is an easy way of doing this or there is an easier way of accessing these methods sorry these properties and that is to use the magic method set so let's say for example instead of the set job title like so we would instead have public function underscore underscore set as you can see the ID has picked that up passing in two arguments the first is the name and the second is the value hmm excuse me so the name is the property that we wish to set so we would do this then the arrow symbol and then we would do name is equal to the value like so now I'm just gonna change that because that will throw an error because we don't have that method anymore so this job title I'm just gonna put that back to job title like so now and I'm gonna remove I'll keep that in at the moment so public function underscore underscore set passing in the name of the property that we wish to sit as well as the value we wish to set to so in this case we would pass in job title and then the name or sorry the value of the job title and that will just basically do what that's done but here in a dynamic fashion so we are accessing and setting this this property dynamically so let's scroll to the bottom and where we have gained job title tester so let's save that and see what happens and let's just change that to be a get job title and run this and we can see that we have changed the value of the job title to tester let's just explain what we've done here so Jane arrow symbol and then job title is equal to tester now if you saw the previous tutorial you will notice that Janne title the job title is actually set to private so how we actually do how are we changing the the value of that job title to be testa if it's private well it's this underscore underscore set method that is doing that it basically it says it's this gets called whenever a property is trying to be accessed that doesn't have that it is inaccessible from its scope so basically overloading basically means that we're creating dynamic properties and methods so a dynamic Pro entity in this sense would be processed fire a magic method to establish itself within the class so we have a private job title which means that it cannot be accessed from outside of the scope of the class but if it is accessed from outside the scope of the class then this set method is called so if I was to just comment that out for example and then run this we will get an error because we're trying to access a private property job title from outside of the scope of the class so let's just put that back and run that again so now we have tester let's just clear that down just to give us some more room now likewise with the setter with this set method we also have a get method - so let's just scroll down here where we have this on line 54 so echo get job title we don't need to do that anymore if we were to just jump to that we can actually now remove this and instead of having get job title we just do let's just remove all that underscore underscore and then get and the name is like like this here where this is the name of the property that we wish to get so we're just going to do a return of this sorry this name like so and let's scroll to the bottom here and change that from this get job title because this doesn't doesn't exist anymore there is no method called get job title anymore it's just these two getters and setters let's just change that to just job title and save that run this and we should see that we have the tester job title - let's scroll back up to here let's remove this and save that just to demonstrate we should now get an error that says that we're trying to access a private property a job title let's put that back and run that we can see that we now have tester again now this is very useful if for example you have a class that has a lot of private properties that you wish to access and also if if you allow access to these properties from outside the scope of the class and they also need they all need to conform to some sort of format so for example in the setting in the set you might want to make ensure that they all have uppercase values for instance and you would do that like this and we could also do private I can spell it right private I know let's just call this job number for instance and scroll down to the bottom we can now do oops sorry Jane job number is equal to five for instance and we can echo out job number I'm gonna put just put in a new line in between two and run that again now we can see that we have the job title as well as the job number and even though these are private properties we can access these by by using these set and get magic methods these are methods that allow for property overloading now that's you know the good the the good reason of doing this is is to allow these these properties to be accessed from outside the scope of the class but there is a trade off and the trade off is that now you've you've removed the the documentation that the IDE that you're that you're I'm using netbeans here that your IDE has so for example if I was to do Jane and then CI I haven't there is no knowledge here of job title and job number because and it's because if I was to remove that go back up it's because these are set to private and so the ID knows that these are private and therefore it it doesn't have any idea of of what these properties should be if I was to put that to public for example and then scroll down here and do jiaying I can now see that we have job title and in fact I can go even further than that I can actually add some documentation to this so if I was to do put in some comments let's say this is a string and this is take a love of job and save that scroll down we can now see that we have Jane job title and in the documentation we have title of job type is a string so that's the trade off the trade off is basically that we have no documentation if we're using this these magic methods and also where we are exposing our our properties we're allowing these properties to be overloaded from outside the scope of the class and perhaps we don't want to do that perhaps we want to keep these encapsulated within the within the classes themselves but that is property overloading I hope you found it useful if if you've got any questions then please leave them in the comment section below like the video if you found it helpful share it around if you think others will do so as well thanks again for watching follow me on twitter my name's PFW d and i shall see you again next week cheers bye oh and welcome to the next PHP object-orientated programming tutorials today we're going to look at class constants now these are values that never change within a class so these aren't variables these aren't properties these aren't methods these are constant values now in the previous tutorial I should mention we looked at getters and setters and magic methods to do property overloading basically these are the underscore underscore set and underscore underscore get if you haven't seen that video I would definitely recommend checking it out I'll put a link in the description below as well as in the top right-hand corner I recommend viewing that because this this tutorial might become a little bit more clearer okay so class constants then like I said these are values that never change within a class and you can't also you can't change them from outside the class either now these are very handy when you're you've created a class that perhaps uses an API and the API has a value perhaps it's got a token or key and that value never changes throughout the lifespan of the the class and therefore it is a constant value and so you can set that you in hard code that within your class just for demonstration though we're going to create some some constant values here and we're going to start by using the Const keyword and we need to have a the name of the constant that we wish to set so in this case we're going to have just company company name like so we're gonna equal that to just acme like that and we're gonna put the semicolon at the end save the file and you can see that the IDE here my ID has noticed that this is a Const keyword and therefore that is italicized so this is in italics and I've also uppercase this it's just basically my my standard whenever I write constants I always put them as uppercase and the properties are always camel case and in the consonants I've always got an underscore to denote a space okay so we can access this from outside of the scope of the class and we do this by I was to scroll right to the bottom we could do this by just echoing out employee oops and then colon colon company name and let's just save that and run the file okay so we can see that we've got Acme that has been displayed and so we're using employee because that's the class colon colon and then the constant name and we echoing it out now we can also access the variable from inside the scope of the class I'm just going to comment that out scroll to the top and I'm going to do echo and because we're inside the employee we need to do self colon colon company name and so this is just gonna echo out the company name when the class is instantiated so let's just run that and we can see that it has echoed that out as well let's just remove this line here and save that now what we can't do however is we can't set this constant value because it is a constant value once it's been set in the class it can never be changed so we can't do this for example we can't save that in fact my IDE has has put a nice read under underscore or line underneath here to say that we could just can't it just fails and it's because this equal sign is unexpected because constants can't actually have their values change so let's just clear that down whoops and run the run this just to demonstrate that we have an error so it's unexpected equal sign and that's because we cannot change its value once it's set in the class now we have two classes here we have the employee and the person class and the employee extends the person class so let's just create a constant within the person class and see if we can access it from the employee class so we're going to create constant again so the Const keyword and then the name and we're just gonna have i underscore color and then we're gonna equal that to be brown save that now can we access the eye color from within this employee class well it's extending so we must have access to these variables these properties and these methods because they're all public we also have access to the constant and we use what's called a parent keyword to to access parent attributes or parent constants as well as parent methods and we do that like so if we were to do echo parent and then eye color we can access the eye color from this constructor and notice here we have parent who on current construct this is running the parent constructor from this constructor so let's just clear that down run this as an example and we can see that we have Brown we've accessed the brown constantly eye color constant from within this constructor from within the employee person now if we were to access eye color again but from within whoops the person class we would need to use self because we're referencing the the class that we're in so self eye color let's just echo that out as a demonstration whoops if I misspelled echo correctly there we go so let's clear this down and run it again we can see that we have echoed out Brown again like so and we can't we cannot do we can't like I said before we can't actually set the value but we can access the value from outside of the scope of the class so we can do employee : : I color and the reason why we can do that is because of the class employee extends if I was to echo that extends the the person class so employee has access to the I color okay so that's class constants this is a quick demonstration of how we can create class constants and how we can use them if you found this tutorial helpful then do like the tutorial and share it around if you think others might find it useful too if you've got any comments or questions then leave them in the comment section below and I'll try and get stew back to them back to you as soon as I can thanks again for watching I shall see you again next week thank you bye and welcome to another PHP tutorial regarding object orientated programming in the previous tutorial we looked at the constant keyword and talking about what is a constant value and how we can access it and so forth if you haven't done so already do check that video out I'll leave a link in the description below as well as in the top right-hand corner today however we're gonna look at the static keyword what it means how to use it and why it's there so before I get into that let's just tidy this up let's just remove those three lines because we don't actually need them now a static keyword basically means that while you put the static keyword before a property or a method so in the same way as we have public scopes and private scopes as well as the constant keyword we would have a static keyword and then the property name and then its value or we would have a static method so it would be our has a static function and so forth now static properties and Static methods can be accessed from outside of the scope of the class in the same sense I suppose as these constants so employee colon colon I color can be accessed from outside the scope of the class but the big difference here well apart from it being constant and a property is that static methods and Static properties can be accessed without instantiating the actual class itself so for example let's just create a basic static property so static and then we're gonna have we're just gonna do employee number for example is this isn't a very good example but I'll just put that in for now so employee number now what we can do is in without actually instantiating this class we can now do echo employee and we can access this employee number like so let's save that and let's run this file so we can actually now get the the employee number without instantiating the class that there's no instantiation it's not there's there's this isn't being touched so the constructor isn't being touched at all we haven't instantiate this class we are literally just pulling out this property from the employee class now likewise with the properties we can also do these with the with with the methods so for example I could have a a public static whoops if I can spell it public static function and we're just gonna have this as get ploy e number number yep and we're just going to return self because we have to reference itself employee number and we scroll down to the two here and instead of doing like we we had done before instead of doing variable Jane is equal to a new instance of employee and passing in the variables we can literally do this we can do employee : : oops get employee number like that let's save that and run this so we've got the same same value here 100 and we haven't instantiated the class at all we are literally running a static method from the the employee class which is called the get employee number now notice I've got a public static so I've actually put the visibility scope in front of or behind of the the static keyword now if I was to change that to private what do you think is gonna happen if I put that to private and save that we'll scroll down here let's just clear this screen to give us some room and then run that it's gonna throw an error it's actually gonna say that we can't access this private method from the context of nothing now this basically means that we can have static methods as well as I suppose static properties because we can also do a stack up sorry a public static due on the right side here public static method excuse me it means basically that we only this class can actually access this static variable so if we were to just comment that out for example we could do this if we change that that is still private what we could do is in the constructor here is we could to do self employee number in and also we can also do self get employee number which I'm just gonna demonstrate let's just echo that out so scroll to the bottom and then we're going to do a new instance so Jane is equal to a new instance instance of employee passes in all of these variables I'm just gonna put that as test tests also test for now we don't need to pass the gender because that's optional save that and we should have the return or the echo because that's in the constructor so at 100 so let's just jump into that class again so notice here on line 24 we are actually echoing out the get employee number method which is where do I put it up here so we can actually restrict the visibility of the static keywords as well these static items so when when is it a good time to use static properties and methods this is a bit of a controversial topic I suppose I haven't really given a very good example employee number will be a variable it's it can change and it really shouldn't be defined statically like this so when is a good way a good time to use static keywords well in my opinion when you're using things like objects that contain variables that need to change and need to be encapsulated within itself I would instantiate the object and it's done and then and then pass the variables through perhaps in a constructor or some setter methods and so forth and I wouldn't actually declare them as static I would only use static the static keywords when I'm actually using for example a knob class that needs to have that has a series of utility methods so for example we could have instead of get employee number we could have generate generate generate pay slip for example right because that's actually doing something that's actually a tool that we can use we don't have to instantiate this employee class in order to generate the pay slip we can literally just generate the pay slip and if I was to do change that to something a little bit more user-friendly let's say this was PA ye number for example and changed that whoops then it makes a little bit more sense so let's just change that we're going to need to just remove that down here scroll to the bottom here and we're going to do an echo of employee and it is whoops I'm bear with me in it let's just scroll back up what I'm I missing oh yes sorry I need to change that to be public to Li all the scope there we go so if I was to echo that now yeah we have 100 now that could just be the you know that could generate a PDF perhaps of the payslip one thing I should note though is in this in this static method what we can't do is we can't use or reference this within this static method if I did this let's say job title for example job let's use job number let's just set job number to be 10 and then return that as an example this is gonna throw us an error so let's just do it clear and then run that now we have an issue here because we're using this when not in the object constants context and basically that means is that the object itself hasn't been instantiated so that's something to definitely look out for if you're using static methods in this way you cannot actually use the properties or access the properties in the same way as a normal instantiated class okay so here we're we're using return this job number this will only work if the job number is a static job number and woops sorry I the old magic mouse went a bit nuts there if I was to say that and change this to be self job number and save that clear this down oops and then run that again now we have the job number which is 10 another thing I should note is inheritance so the employee extends the person so if I had a static variable on here so let's say we had static we'll have a we'll set this to be protected just as a an example of the variability scope so a protected static and we're going to just we're just gonna call this blood type and it's just gonna be a positive and save that now what we can do is we can access this this blood type from from another static method so we could just change this to be self and then blood type so we can actually access the blood type static variable by calling self and the reason why we can do this is because it extends the person let's save that let's run this again and we have a plus so that's static keywords and Static methods and Static properties if you've got any questions or comments then please let me know put them in the comment section below if you've if you found this helpful then give it a thumbs up share it around maybe others might find it useful to follow me on twitter my twitter handle is at PFW d I've also got a Facebook page for this how to code well Channel so do check that out put the links in those both of those social media things in the description below thanks again for watching and I shall see you again next week thank you bye you
Info
Channel: Peter Fisher
Views: 98,238
Rating: undefined out of 5
Keywords: php tutorial, php oop, php oop tutorial, php oop full course, php full course, php classes, php object oriented, php object oriented programming, php object oriented programming project, php object oriented tutorials, php class inheritance, php oop for beginners, php oop tutorial for beginners full, php oop for dummies, php books, php class contants, php static, php extends, php oop course, php oop course free, learn to code, how to code, how to learn to code, coding
Id: fiMo0zNdrt4
Channel Id: undefined
Length: 82min 46sec (4966 seconds)
Published: Tue Jan 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.