To further demonstrate that $drone2 is indeed of type
Corporate_Drone, its employeeid member was also reassigned.
The __clone() Method
You can tweak an object??™s cloning behavior by defining a __clone() method within
the object class. Any code in this method will execute during the cloning operation.
CHAPTER 7 ?– ADVANCED OOP FEATURES 197
This occurs in addition to the copying of all existing object members to the target
object. Now the Corporate_Drone class is revised, adding the following method:
function __clone() {
$this->tiecolor = "blue";
}
With this in place, let??™s create a new Corporate_Drone object, add the employeeid
member value, clone it, and then output some data to show that the cloned object??™s
tiecolor was indeed set through the __clone() method. Listing 7-2 offers the example.
Listing 7-2. Extending clone??™s Capabilities with the __clone() Method
// Create new Corporate_Drone object
$drone1 = new Corporate_Drone();
// Set the $drone1 employeeid member
$drone1->setEmployeeID("12345");
// Clone the $drone1 object
$drone2 = clone $drone1;
// Set the $drone2 employeeid member
$drone2->setEmployeeID("67890");
// Output the $drone1 and $drone2 employeeid members
printf("drone1 employeeID: %d
", $drone1->getEmployeeID());
printf("drone2 employeeID: %d
", $drone2->getEmployeeID());
printf("drone2 tie color: %s
", $drone2->getTieColor());
Executing this code returns the following output:
drone1 employeeID: 12345
drone2 employeeID: 67890
drone2 tie color: blue
198 CHAPTER 7 ?– ADVANCED OOP FEATURES
Inheritance
People are quite adept at thinking in terms of organizational hierarchies; thus, it
doesn??™t come as a surprise that we make widespread use of this conceptual view to
manage many aspects of our everyday lives.
Pages:
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276