note 45132 added to migration5.incompatible
| From: | derrell dot lipman at unwireduniverse dot com | Date: | Fri, 27 Aug 2004 02:14:23 +0000 |
| Subject: | note 45132 added to migration5.incompatible | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-75394@lists.php.net to get a copy of this message | ||
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;
}
----
Manual Page -- http://www.php.net/manual/en/migration5.incompatible.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45132
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45132&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45132&report=yes
Search -- http://master.php.net/manage/user-notes.php