Join Jon Peck for an in-depth discussion in this video Overloading property access, part of Object-Oriented Programming with PHP.
Within PHP, overloading means to dynamically create a property or method that has not been declared, or is not visible in the current scope. When I say create, it's figurative. PHP will act like they exist and can be accessed, even though in reality they don't. These dynamic entities are processed using magic methods. Object properties have two events that can be invoked when accessing a missing or out-of-scope property: when a script _gets a property, and when a script _sets a property. They can be useful to do things like use a method or function to determine the value to be returned, or execute some sort of failsafe behavior when trying to set a property that doesn't exist.
Both the get and the set magic methods have public visibility, so you don't have to specify scope when declaring them. In the Address class, a magic _get method would be useful for calculating a missing postal code. For example, if the postal code was null, but there is a city in subdivision, I could attempt to look up the value in a table. However, as the postal code is currently public, the magic _get method won't be triggered. Therefore, we'll change the scope of the postal code to protected, in order to be able to use the magic _get.
I'll demo the database lookup later. For the time being, we'll put in a placeholder and focus on magic methods. Open the Address class, then change the scope of postal code to protected. Remember to prepend the property name with an underscore (_) to visually indicate the scope. Then start editing the file after the last property declaration. Add a placeholder function for the database lookup for the postal code. As there is no need to expose this function outside of the address class, protected as well. protected function _ postal_code_guess, return LOOKUP. Add proper PHP documentation, guess the postal code given the subdivision and city name, to do.
Replace with a database LOOKUP. Now that you have a postal code placeholder, you can now leverage the magic _get method. They take only one argument, the name of the property in the form of a string. Place the Magic _get method above the protected method, postal_code_guess. function__get name. Then, documentation Magic _ get. Name as a string, and returns next.
Insert the logic for the special behavior for the postal code property. Postal code lookup if I set, if no postal_code, and set postal code to _postal_code_guess. After the special condition for postal code, you'll expose any protected properties that start with an underscore as read-only. Remember, this will get around scope restrictions.
So, only do this if you are aware of the potential consequences, such as accidentally exposing internal data. In this case, there is no harm in exposing all protected properties as read-only. Attempt to return a protected property by name. You cannot call a property using a concatenated string, so create a variable that starts with the underscore followed by the name. $protected_property_name=_ followed by $name. if property_exists in this with the property name, return this protected_property_name.
If the property does not exist, trigger a PHP error and return null. trigger_error Undefined property via _ get, followed by the name. return NULL. If you were to test the script now, you would get a failure when you attempted to set the postal code, as it is currently protected. Therefore, you will also need to define a magic _set to handle that circumstance.
Beneath the magic _get, define a magic _set method. The magic _set method has two arguments. The first is the name of the property as a string, and the second is a mixed value to be set. function__set name, value. Add the PHP documentation, string for name, mixed for value. Add the check for the postal code property, and allow anything to manipulate it. In this example, stick with a simple if statement, as there is only one logical check.
If there were multiple property names that you are checking for, a switch statement may be more readable. Allow anything to set the postal code. If postal code is the name, this name = value. return. If the property is not postal code, trigger contextually appropriate error. Unable to access property; trigger error. trigger_error ('Undefined or unallowed property via _ _set (); followed by the name); Save the address class, then open the demo file. At the end, remove the unintentional failed display of address ID. Then, let's test the new magic _get method by unsetting the postal code, and displaying the result.
echo <h2> Testing magic _get and _ set; unset ($address postal_code); echo $address_display();. Save the demo file, then refresh your browser. The first instance of the postal code shows up as expected, and the second instance with no postal code now shows the word LOOKUP. In the next video, you can set the time created, leveraging a construction magic method.
- 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