Cloning and comparing objects
Video: Cloning and comparing objectsSometimes, you will want to make a copy of an object. There are many reasons to do this, including making a backup before performing an operation, or as a shortcut for defining a similar object. To create a copy of an object, use the clone keyword. As a demonstration, make a copy of the park address, open the demo script, and navigate to the bottom. Add the following lines: echo
Cloning AddressPark$address_park_clone = clone $address_park. And then, we'll debug the clone.
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
Cloning and comparing objects
Sometimes, you will want to make a copy of an object.
There are many reasons to do this, including making a backup before performing an
operation, or as a shortcut for defining a similar object.
To create a copy of an object, use the clone keyword. As a demonstration, make a
copy of the park address, open the demo script, and navigate to the bottom. Add
the following lines:
Cloning AddressPark $address_park_clone = clone $address_park. And
then, we'll debug the clone.
To check the differences between clones, I'm going to compare two objects. Comparing objects is a lot like comparing primitives, such as strings and floats. I can use the comparison operator, double equals (==), which will check to see if the properties are the same. If I need to be more strict, I can use the identity operator, triple equals (===), to also check if both objects are instances of the same class. At the end of the script, add echo $ address_park_clone_ is. And then, a ternary statement. $address_park == $address_park_clone ? : not ] a copy of $address_park. Save the demo, and refresh the browser.
Visually, every property is the same, and the final line indicates this. However, the time created isn't necessarily the same, especially if one object is based on an old record from the database. To deal with this, I'm going to use the _ clone() magic method in the address class. If the method exists, it will be called after object cloning. As an example, I am going to use this to reset the time created. Let's go back to the address class. Then, after the properties, add the clone method, which takes no arguments. Remember to add proper documentation.
For the demonstration, I will explicitly set the time_created and time_updated to obvious values. Save, then switch to the browser, and refresh. The time_updated and created have been set to the new values, and the final line states that $address_park_clone is not a copy of $address_park. Return to the clone function, and set time_ created = time, and time_updated = Null. Now that I've demonstrated object cloning, I am going to discuss how to reference objects.
Referencing can be similar to cloning, but there are enough differences to make it worth exploring on its own.
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.