Abstract Classes - PHP Object Oriented Programming Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's going on is clarity key and in this video we're going to cover PHP abstract classes an abstract class is a class that must have at least one abstract method and the abstract method is a method that is defined but it's code is not implemented so it's up to the extending child classes to implement the code of the abstract method the abstract class is created with a keyword abstract followed by the keyword class and then the name of the class I'm gonna name it element and let's go ahead and now create the abstract method with the same keyword abstract give it a public axis and then function and the name of our method it's going to be a remind I'm also going to use Colin string to make sure this method returns a string and that's it we have now created an abstract class with one abstract method and as you can see there's no body code of this method so now we're going to create a child class which is going to extend this method and then implement the code or the body of the actual method so let's go ahead and do that right now class I'm gonna name the class water extends element this method inside the child class must be named exactly the same so we're gonna say public function remind and then call in string and now we're going to define the body of the element and remember that it must return a string so all we're gonna say is return and then the message water element okay so at this point we have created an abstract class with one abstract method we have also extended the parent method with a child class named water and we have also implemented the abstract method inside the child class and created it body so now we're going to test out our class by creating an object so let's create a new variable named element and say new water to instantiate our water child class and then all we have to do is say echo element remind and that should print out our message water element so let's go ahead and open a browser and we have water element now let's go ahead and change our child class a little bit so instead of hard-coding the water element string here we can actually use two methods to get the current class name and we can also use a method to get the parent class name so let's delete this here and we're going to use the method get' underscore class and then this which is referring to this object so that is going to get the current class name which is water and then let's concatenate the string with a dot and use another method you get parent class of this object so that is going to get the parent class of the current object which is the element so it's going to print out exactly the same it's going to print out water element just like before okay let's go ahead and save this and test this in the browser and as you can see we still have water element so by using these methods instead of hard-coding the string of the class names ourselves we can now create more child classes and the functionality will be done for us so we don't have to change the actual string that is printed out and now let's go ahead and actually create three new child classes name the second one fire the third one Earth and the fourth class air and leave all the functionality exactly the same so now we can create or instantiate these four classes here fire earth and air let's also go ahead and print out the constant BR have some space in between our messages and now let's go ahead and view the whole thing in a browser and now we have order element fire element earth element and air element so the child name classes have all changed without us having to change the code now let's actually create the message for every element that we have here so for that we're going to create a private property and name it message and here we're gonna have a meaningful message related to each element so for the water element we're gonna remind ourselves to drink eight cups of water every day and become aware of the motions let's also modify the code here a little bit and we're still gonna be returning a string we're just gonna add the message by concatenating it with Colin and just adding this message so that is going to print out the actual message here and let's go ahead and also change the rest of the classes as well so I'm just gonna copy this code and paste it here and let's go ahead and add the message to all the classes as well so for the fire element we're gonna have Oricon creative and passionate projects which is a message related to the fire element the earth element private message the message from the earth element is going to be developed body awareness and healthy posture because the earth element is related to the body and then finally the air element which is associated with the breath and the mind the message is going to be developed breath awareness and mind concentration so now we have a unique message that will be displayed by each of the elements with the method remind ok let's go ahead and view this in the browser and now we have a useful message from every element reminding us of healthy habits so we've actually created a very useful program now we're going to add a second abstract method to our parent element class and we're gonna set its access to protect it which just means that this method is only going to be accessible inside of the parent or child classes and we're just gonna create a method that will let us set the custom message instead of the default one and it's gonna take in custom message as it properly let's also create the new property custom message inside of our parent class so that we don't have to define it in all of our child classes and let's set the access to private so that it can only be used inside of the classes so all the child classes will now have custom message as a property okay so okay so now let's go ahead and implement the new abstract method that set custom message inside all of the child classes and we can actually use and we can actually set the access to public instead of protected and this will be perfectly legal because the extending child classes of abstract parent class when now let's go ahead and declare this new property custom message inside of our parent class so that we don't have to define it in all of our child classes I'm gonna set its access to protected and I'm gonna name it custom message and now we're ready to implement set custom message method inside of all of our child classes and here we're actually going to set the access to public instead of protected because the extending child classes of the abstract method must have a less restricted access so they can either be protected or public but they cannot be private and this is just to demonstrate how we can modify our extending classes a little bit when defining the abstract methods so we can use public instead of protected and this will allow us to use these methods outside of the classes okay so public function set custom message it's gonna take in custom message as a parameter and now let's go ahead and define the body of the method and we're simply going to set this custom message to custom message so we're setting the property custom message that we define here in the parent class and we have the custom message accessible inside of the child class because the child class inherits all of the parent properties and methods and we're setting this property to the variable that is passed down from the user okay so now we can set the custom message and override the default message here and to implement our set custom message method properly we're gonna modify a remind method a little bit so instead of returning the whole thing let's just go ahead and echo out our child class and parent class names and then on the new line we're going to return the message and first we're just gonna check if is set this custom message and if it's set which is gonna return this custom message otherwise we're gonna return default message okay so our methods are now ready so let's go ahead and copy and paste them in the rest of the classes and our set custom message method should not work so let's go ahead and test this and we're going to set custom message before calling the remind method so we're gonna say element set custom message and then any message that you like I'm gonna say game emotional mastery and this will overwrite the original message or default message which was drink eight cups of water every day and become aware of emotions so let's go ahead and view this in a browser and now we have gained emotional mastery so we can also do this to all of the other child classes as well and it will work in exactly the same way now let's go ahead and add a construct method to our parent element class and the construct method will be called automatically when the object is instantiated and we create a construct method with two underscores and the name construct and it will take in an array four keywords and the default value of keywords is just gonna be empty array so in case no parameter is specified which is gonna have an empty array so the keywords so that makes the parameter keywords optional and here we're just gonna save these keywords equals keywords now let's create the property protected the Oracles array so what we're gonna be doing here is we're gonna be associating some key words any key words that come to mind that are associated with each element so with water we can for example use some key words like fluid deep shallow flowing etc and now let's add another abstract method that will actually get or print out the key words for us so we're gonna name it get key words and inside of the child class we're actually gonna add an optional parameter inside of the method which is another thing we can do when implement in abstract methods okay so let's go ahead and add this method in the first child class water element public function we have keywords and so here we're going to add an optional parameter which is going to be a separator and we're gonna set the default value to a comma so the keywords will be separated with a comma by default if no parameter is specified and now we're just gonna add that for each loop to loop through the keywords array and print out all the keywords first we're gonna say like a few words and then for each keywords as pure so we're looking through the keywords array and inside of the for each loop before printing out the keyword we're gonna test to see if we are at the end of the keywords array because we don't want to print out the separator at the end of the last keyword so here we're gonna test for this by saying keyword not equal and this keywords and that's gonna test to see if we're not at the end of the array and if we're not at the end of the array we're gonna print out keyword with a separator otherwise which is gonna print out a keyword by itself and that's it our get keywords method should now work so let's go ahead and copy and paste this method inside the rest of the child classes okay so first we're gonna add the array of keywords inside of our method constructor which means we're gonna add it here so for the water element I'm gonna add some keywords related to water like flow emotion fluid deep and intuition and now all we have to do is call our get keywords method element get keywords let's use the BR constant here and now let's view the whole thing in a browser and keywords but it's not printing the actual keywords so let's see what we did wrong and we forgot the echo keyword here so let's just go ahead and add that and add it to all the other child classes as well so that we're printing out the actual keywords okay now let's test this in the browser and now we have all the keywords being printed out so at this point you can just play around with the rest of the child elements I'm just gonna add my own keywords that I associate with fire earth and air I'm gonna add yours too the earth element and air but feel free to add your own keywords that you associate with all these elements and we can also set a custom message for all the elements as well so here we can say something like spend more time in nature okay and let's go ahead and print out the keywords or all the rest of the channel classes and now view this in the browser and so now we have all the keywords associated with each element being printed out as well as a custom message for earth element and that's it for this video about PHP abstract classes as well as some other additional object-oriented features if you guys like this video please like share and subscribe and I'll see you next time clever take it out
Info
Channel: Clever Techie
Views: 3,946
Rating: undefined out of 5
Keywords: abstract classes php, php abstract classes, php abstract class, php object oriented programming, php object oriented programming tutorial, php tutorial, clever techie php, abstract class, abstract classes
Id: BzAVqZWB_Zo
Channel Id: undefined
Length: 18min 24sec (1104 seconds)
Published: Wed Nov 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.