Overriding methods, properties, and constants
Video: Overriding methods, properties, and constantsSometimes, 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.
Viewers: in countries Watching now:
Whether you're enhancing or optimizing existing code or just starting from scratch, there's never a better time to start integrating object-oriented design techniques. This course shows how to integrate the principles of object-oriented programming into the build of a PHP-driven web page or application. After an overview of what objects and classes are and why they should be used, author Jon Peck dives into creating and instantiating objects, then defining the class relationships and interactions that will form the basis of your coding arsenal. The course also shows how to leverage PHP objects and implement design patterns, and looks at steps you can take to continue adding to your programming tool belt.
- Historical overview of object-oriented PHP
- Defining classes
- Creating a method/object context with $this
- Accessing classes without instantiation
- Creating a database class
- Extending and abstracting classes
- Cloning and comparing objects
- Error handling with exceptions
- Implementing design patterns, such as the factory and strategy patterns
Overriding methods, properties, and constants
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.
Find answers to the most frequently asked questions about Object-Oriented Programming with PHP .
Here are the FAQs that matched your search "" :
- Q: I'm not seeing warnings or errors in my environment like the video; why not?
- A: Your PHP configuration is probably configured not to show them to you. This is often true on commercial web hosts and is often the default. Fortunately, there are multiple ways of resolving this.
The easiest way would be to explicitly enable error reporting at the top of the PHP script you wish to debug.
error_reporting(E_ALL | E_STRICT);
Alternatively, if you have access to your php.ini file and you want to always have error reporting on, change error_reporting = to a development friendly value of
error_reporting = E_ALL | E_STRICT
then change display_errors = to
display_errors = On
Finally, if access to the php.ini file is not available, you can add the following directives to your .htaccess or VirtualHost configuration for Apache:
php_value error_reporting 32767
php_value display_errors 1
If you would like a development optimized development environment like the one utilized in this course, see Up and Running with Linux for PHP Developers, here in the lynda.com online training library.
Q: This course was updated on 4/10/2013. What changed?
A: The author rerecorded some of the tutorials to add more background information and better graphics.
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.