Section 8.11. Copying Objects


8.11. Copying Objects

From PHP 5 onward, objects are always handled as references. This means that when you pass an object into a function, any changes you make to it in there are reflected outside the function. For example:

     function namechange($dog) {             $dog->Name = 'Dozer';     }     namechange($poppy);     print $poppy->Name . "\n"; 

Here we define a function that accepts one variable, $dog, then changes its name to Dozer. We then pass our $poppy dog into the function, and output its nameunsurprisingly, it outputs "Dozer" rather than "Poppy". Sometimes it is important to only work on copies of objects, particularly if you don't want to affect the state of the original. To do this, we use the built-in keyword clone, which performs a complete copy of the object. For example, we could use the namechange( ) function above like this:

     namechange(clone $poppy); 

That would create a copy of $poppy and pass it into namechange( ), leaving the original $poppy untouched. Here is the output of the code now:

     Creating Poppy     Creating a poodle     My name is Poppy. If you find me, please call 555-1234     Dozer is no more...     Poppy     Poppy is no more... 

Note that Dozer is still mentionedthat is because the copied object passed into namechange( ) gets its name changed to Dozer; then, when the function ends, the copied object is automatically destroyed by PHP, and its destructor is called. However, $poppy lives on untouched, as you can see from the last two lines.

Internally, the clone keyword copies all the properties from the first object to a new object, then calls a magic method _ _clone( ) for the class it is copying . You can override _ _clone( ) if you want, thereby giving you the flexibility to perform extra actions when a property is copiedyou can think of it as a constructor for a copied object. For example:

     public function _ _clone( ) {             $this->Name .= '++';     } 

That method will be called on the copied object, and will set the copied object to have the same name as the original, with ++ tacked onto the end. So, rather than the clone being called Poppy, it will be called Poppy++. If we clone the clone, it will be called Poppy++++, and so on.

For really advanced functionality, you can also call parent::_ _clone( ) to work your way up the inheritance chain and call the _ _clone( ) method of the parent class. Again, all the copying of data is already done, so all the _ _clone( ) method would be required to do is make any last-minute tweaks to the copy. Here's how that looks:

     abstract class Dog {             public function _ _clone( ) {                     echo "In dog clone\n";             }     }     class Poodle extends Dog {             public $Name;                     public function _ _clone( ) {                     echo "In poodle clone\n";                     parent::_ _clone( );             }     }     $poppy = new Poodle( );     $poppy->Name = "Poppy";     $rover = clone $poppy;



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net