Customizing object construction
Video: Customizing object constructionStarting 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.
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
Customizing object construction
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.
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.