php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31321 an object given to a function as a param. by value(!) is passed by reference
Submitted: 2004-12-28 14:39 UTC Modified: 2004-12-30 16:56 UTC
From: i25506 at informatik dot fh-wuerzburg dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.0.1 OS: Win32 and Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: i25506 at informatik dot fh-wuerzburg dot de
New email:
PHP Version: OS:

 

 [2004-12-28 14:39 UTC] i25506 at informatik dot fh-wuerzburg dot de
Description:
------------
If you try to give an object as parameter to a function by value, the object is not copied but passed by reference. So if you change the object in that function, you change your original object - after the function is returned you get still the changed object.
Shortly said:
function($myObject) and function(&$myObject)
are the same thing, which is wrong. All PHP-versions <5 i've had before (many 3.x and 4.x) does right.

Reproduce code:
---------------
<?php
function Test_FunctionParameter($myobj)
{
  $myobj->testattribute="Changed!";
}

class TestClass
{
  var $testattribute;
}

$testobj1=new TestClass;

$testobj2=new stdClass;
?>
<html>
<head>
<title><?php echo basename(__FILE__)."-".phpversion();?></title>
</head>
<body>
<h1><?php echo basename(__FILE__)."-".phpversion();?></h1>
<table border>
<tr>
<th></th><th>Original object:</th><th>Object after passing it by value(!) to a function which changes the attribute:</th>
</tr>
<tr>
<td>1. Self defined class</td>
<td><pre><code>
<?php
$testobj1->testattribute=3.14159265;
print_r($testobj1);
?>
</code></pre></td>
<td><pre><code>
<?php
Test_FunctionParameter($testobj1);
print_r($testobj1);
?>
</code></pre></td>
</tr>
<tr>
<td>2. pre defined stdClass</td>
<td><pre><code>
<?php
$testobj2->testattribute=array(1, 2, 3, 4);
print_r($testobj2);
?>
</code></pre></td>
<td><pre><code>
<?php
Test_FunctionParameter($testobj2);
print_r($testobj2);
?>
</code></pre></td>
</tr>
</table>
</body>
</html>

Expected result:
----------------
1. Self defined class 
Original object:
testclass Object
(
    [testattribute] => 3.14159265
)

Object after passing it by value(!) to a function which changes the attribute: 
testclass Object
(
    [testattribute] => 3.14159265
)

 
2. pre defined stdClass 
Original object:
stdClass Object
(
    [testattribute] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

)

Object after passing it by value(!) to a function which changes the attribute: 
stdClass Object
(
    [testattribute] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

)


Actual result:
--------------
1. Self defined class 
Original object:
testclass Object
(
    [testattribute] => 3.14159265
)

Object after passing it by value(!) to a function which changes the attribute: 
testclass Object
(
    [testattribute] => Changed!
)

 
2. pre defined stdClass 
Original object:
stdClass Object
(
    [testattribute] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

)

Object after passing it by value(!) to a function which changes the attribute: 
stdClass Object
(
    [testattribute] => Changed!
)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-28 15:25 UTC] derick@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

Also read the migration guide.
 [2004-12-30 16:56 UTC] i25506 at informatik dot fh-wuerzburg dot de
O.K., I understand what You mean, since the new object-model in PHP5, variables not holding the object itself, but a handle to the object. In my understanding that is similar to a reference on a "normal" variable. If I do '$a=$b;' on a "normal" variable $a holds a copy of $b. '$a=&$b;' get's $a a reference to $b. If I do '$a=$b;' where $b holding a object-handle I don't get a copy of the object, I only get a copy of the handle - but what is in that meening '$a=&$b'?. This is in my eyes very inconsistent.
The manual about 'function()' says 'function($a)' passes $a by value and there is no hint to the new meaning on the PHP5 object-model, so I made this mistake...
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 04:00:01 2026 UTC