Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Sometimes, it's useful to be able to change how a child class works. To do that, I'll use an override which is just re-declaring a method, property, or constant. If I was to override a property in a child class, I would just declare the property with the same name in the child. Visibility and overrides are similar to abstraction in that I can change the visibility scope, but I can't make it stricter. Overriding a property is very straightforward. In the child class, declare the property. As a demonstration, open the PARK class, then override the country name with the default, Australia. public $country_name = Australia.
Save, then open the demo script.
At the end, add the following: echo
Instantiating AddressPark, $address_park
= new AddressParkarray street_ address_1 => 789 Missing Circle;
street_address_2 => suite 0, city_name => Hamlet, in subdivision_name => Territory.
We'll echo $address_park, display it, and then debug it. Save, then refresh your browser. You'll see that the country is Australia. Return to the PARK class, and remove the property override, then save. Add the country name to the demo, AddressPark.
Overriding a constant is very similar to overriding a property. Just re-declare it. There is one restriction. I can't re-declare a constant that was declared in an interface. Because it's so similar, I'm not going to demo overriding a constant. Overriding methods, on the other hand, has some unique quirks. The new method must have the same number of arguments as the overridden method. There is an exception with constructors, where you can re-declare more or less arguments. Overriding methods can also access methods and properties from the parent class.
This is very good for defining a child's behavior before and after a parent's behavior. As an example of this, I'll demonstrate a method override on the PARK class display to give it a green background. Let's open the AddressPark class, and declare a new display method. public function display. output = div with the style of background-color:PaleGreen. Use the keyword parent as the class name to reference the parent of the extended class address.
I'll put parent::display, close the div, and return the output. Save, then return to the browser, and refresh. The park address is now green. Next, I will demonstrate how to copy and compare objects.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97405 Viewers
61 Video lessons · 84635 Viewers
71 Video lessons · 68844 Viewers
56 Video lessons · 101302 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.