Cloning and comparing objects


show more Cloning and comparing objects provides you with in-depth training on Developer. Taught by Jon Peck as part of the Object-Oriented Programming with PHP show less
please wait ...

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: echo <h2>Cloning AddressPark</h2> $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 st...

Cloning and comparing objects
Video duration: 2m 51s 1h 57m Intermediate Updated Apr 10, 2013

Viewers:

Cloning and comparing objects provides you with in-depth training on Developer. Taught by Jon Peck as part of the Object-Oriented Programming with PHP

Subject:
Developer
Software:
PHP
Author:
please wait ...