php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49641 call-by-value does not work on objects
Submitted: 2009-09-23 10:52 UTC Modified: 2009-09-23 14:48 UTC
From: alhajaj at yahoo dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.11 OS: Windows XP
Private report: No CVE-ID: None
 [2009-09-23 10:52 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-23 11:29 UTC] sjoerd@php.net
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 14:48 UTC] alhajaj at yahoo dot com
Thanks for the quick reply. I guess I was using the wrong keywords to search on the php.net site for an explanation.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Fri Feb 13 19:00:02 2026 UTC