note 45132 deleted from migration5.incompatible by aidan

From: Date: Fri, 27 Aug 2004 03:50:50 +0000
Subject: note 45132 deleted from migration5.incompatible by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75400@lists.php.net to get a copy of this message
Note Submitter: derrell dot lipman at unwireduniverse dot com ---- Automatic cloning of parameter objects has been removed from PHP 5. In the following example, PHP 4 would not modify $x1, while PHP 5 will. (See the output following the code example.) <?php class X { var $a; var $b; function X() { $this->a = 1; $this->b = 2; } }; function CloneXWithChange($obj) { $obj->b = 3; return $obj; } $x1 = new X(); echo "x1 before: a=" . $x1->a . " b=" . $x1->b . "\n"; $x2 = CloneXWithChange($x1); echo "x1 after: a=" . $x1->a . " b=" . $x1->b . "\n"; echo "x2 after: a=" . $x2->a . " b=" . $x2->b . "\n"; ?> The output of the above script is as follows, in PHP 4 and PHP 5: % php4 /var/tmp/x.php x1 before: a=1 b=2 x1 after: a=1 b=2 x2 after: a=1 b=3 % php5 /var/tmp/x.php x1 before: a=1 b=2 x1 after: a=1 b=3 x2 after: a=1 b=3 Note that $x1->b got modified by PHP 5, but not by PHP 4. To retain the PHP 4 behavior in PHP 5, modify CloneXWithChange() as follows: function CloneXWithChange($obj) { $obj = clone($obj); // ensure parameter object gets cloned $obj->b = 3; return $obj; }

« previous php.notes (#75400) next »