__clone
Like the constructor, __clone is invoked by a PHP operator, in this case clone. This is a new operator introduced with PHP 5. To see why it is necessary, we need to take a look at how objects are copied in PHP 4.
In PHP 4 objects are copied in exactly the same way that regular variables are copied. To illustrate, let's reuse the Person class shown in Listing 13-1 (see Listing 13-3).
Listing 13-3. Using the assignment operator under PHP 4
$x = 3; $y = $x; $y = 4; echo $x. '<br />'; echo $y. '<br />'; $obj1 = new Person(); $obj1->name = 'Waldo'; $obj2 ❶= $obj1; $obj2->name = 'Tom'; echo $obj1->name. '<br />'; echo $obj2->name; |
If the code in Listing 13-3 is run under PHP 4, the output will be as follows:
3 4 Waldo Tom
The assignment ...
Get Object-Oriented PHP now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.