#31321 [Opn->Bgs]: an object given to a function as a param. by value(!) is passed by reference
ID: 31321
Updated by: derick@php.net
Reported By: i25506 at informatik dot fh-wuerzburg dot de
-Status: Open
+Status: Bogus
Bug Type: *General Issues
Operating System: Win32 and Linux
PHP Version: 5.0.1
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
Also read the migration guide.
Previous Comments:
------------------------------------------------------------------------
[2004-12-28 14:39:02] 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!
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=31321&edit=1
Thread (3 messages)