#49641 [Opn->Bgs]: call-by-value does not work on objects
| From: | sjoerd@php.net | Date: | Wed, 23 Sep 2009 11:29:57 +0000 |
| Subject: | #49641 [Opn->Bgs]: call-by-value does not work on objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-142763@lists.php.net to get a copy of this message | ||
ID: 49641
Updated by: sjoerd@php.net
Reported By: alhajaj at yahoo dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Windows XP
PHP Version: 5.2.11
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
http://nl2.php.net/manual/en/language.oop5.references.php
Previous Comments:
------------------------------------------------------------------------
[2009-09-23 10:52:29] alhajaj at yahoo dot com
Description:
------------
when passing an object by value to a function it alters the original
object and not a copy of it. Here is a script to reproduce it. I
included the handling for passing strings which works as expected unlike
passing objects.
Reproduce code:
---------------
<?php
class User
{
var $name;
function User(){$this->name = "tony";}
}
function changeUserByRef(&$user) { $user->name = "changeUserByRef";}
function changeUserByValue($user) { $user->name =
"changeUserByValue";}
function changeStringByRef(&$str) { $str = "changeStringByRef";}
function changeStringByValue($str) { $str = "changeStringByValue";}
$user = new User();
echo($user->name . '</br>' );
changeUserByRef($user);
echo("changeUserByRef = " . $user->name . '</br>' );
$user->name = "tony";
echo($user->name . '</br>' );
changeUserByValue($user);
echo("changeUserByValue = ". $user->name . '</br>' );
echo('<hr />');
$user->name = "tony";
echo($user->name . '</br>' );
changeStringByRef($user->name);
echo("changeStringByRef = " . $user->name . '</br>' );
$user->name = "tony";
echo($user->name . '</br>' );
changeStringByValue($user->name);
echo("changeStringByValue = " . $user->name . '</br>' );
?>
Expected result:
----------------
tony
changeUserByRef = changeUserByRef
tony
changeUserByValue = tony
---------------------------------------------------
tony
changeStringByRef = changeStringByRef
tony
changeStringByValue = tony
Actual result:
--------------
tony
changeUserByRef = changeUserByRef
tony
changeUserByValue = changeUserByValue
---------------------------------------------------
tony
changeStringByRef = changeStringByRef
tony
changeStringByValue = tony
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49641&edit=1