Monday, October 17, 2011

Difference between PHP 4 and 5 - passed by reference

Passed by Reference

This is an important change. In PHP4, everything was passed by value, including objects. This has changed in PHP5 -- all objects are now passed by reference.

$joe = new Person();
$joe->sex = 'male';

$betty = $joe;
$betty->sex = 'female';

echo $joe->sex; // Will be 'female'

The above code fragment was common in PHP4. If you needed to duplicate an object, you simply copied it by assigning it to another variable. But in PHP5 you must use the new clone keyword.

No comments: