Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Starting in PHP5, developers could use a magic method for initializing new objects. Before they were used. It's called _ construct (), and this magic method is Optional. It's flexible enough that even can be empty, which is the same as not having a magic construct method at all. If you don't need to initialize an object, don't use it, as it's just wasted code. Developers shouldn't be paid by the line. In the Address class, I'm going to use the magic construct method to set the creation time. Let's open the Address class. Add the following after the properties, before the Magic _get and _set methods.
function __construct. In its current form, this will have no functional effect, neither positive nor negative. If you are declaring a class, and have no need to customize the constructor, leave it out. In the case of your address, you want to set the time created upon initialization. In between the brackets, add the following line: this ->time_created = time(). Save the address class, then rerun the demo.
In the debug, you'll see that time created is now set. This technically works, but it's impractical to specify each property name when populating an object. Fortunately, I can pass arguments to the _construct() magic method, which I can then use to optionally populate the object. Let's go back to the Address class. data = array. And some documentation. Optional array of property names and values.
Next, add a sanity check by ensuring that the argument is really an array. If not, trigger an error and fail. Ensure that the Address can be populated. If not, array trigger_error. Unable to construct address with a get_class$name.
If there is data in array, if there is at least one value, populate the Address with it. Iterate through the array. Later, you will want to be able to set the time created and time updated from an existing record. So, include logic for these protected properties that will allow them to be sent. Special case for protected properties, if in_array name, array time_created, and time_updated, $ name start with an underscore (_).
Finally, set the property. The existing Magic _set() method will trigger an error if you attempt to set an invalid property. $this->name = $value. You can now create a new address object with an array of properties and values. Save the Address class, then open the demo. Start with a title. echo h2 Testing Address __construct with an array. Next, create a new address object, but this time with an array as a property.
$address_2 is a new Address, which will populate with an array. street_address_1 is 123 Phony Ave. The city_name is Villageland, the subdivision_name is Region, postal_code is 67890, and the country_name is Canada.
Finally, display the result. echo $address_ 2 display. Save and test in your browser. You have laid the groundwork that will allow for easy object population from a database record that has been returned as an array. Next, you can simplify address displaying by defining how the object is rendered as a string.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92296 Viewers
82 Video lessons · 104083 Viewers
71 Video lessons · 75463 Viewers
56 Video lessons · 106833 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.