ID:               49641
 User updated by:  alhajaj at yahoo dot com
 Reported By:      alhajaj at yahoo dot com
 Status:           Bogus
 Bug Type:         Scripting Engine problem
 Operating System: Windows XP
 PHP Version:      5.2.11
 New Comment:

Thanks for the quick reply. I guess I was using the wrong keywords to
search on the php.net site for an explanation.


Previous Comments:
------------------------------------------------------------------------

[2009-09-23 11:29:57] [email protected]

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

------------------------------------------------------------------------

[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

Reply via email to