#31321 [NEW]: an object given to a function as a param. by value(!) is passed by reference
| From: | i25506 at informatik dot fh-wuerzburg dot de | Date: | Tue, 28 Dec 2004 13:39:04 +0000 |
| Subject: | #31321 [NEW]: an object given to a function as a param. by value(!) is passed by reference | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-71157@lists.php.net to get a copy of this message | ||
From: i25506 at informatik dot fh-wuerzburg dot de
Operating system: Win32 and Linux
PHP version: 5.0.1
PHP Bug Type: *General Issues
Bug description: an object given to a function as a param. by value(!) is passed by reference
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 bug report at http://bugs.php.net/?id=31321&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=31321&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=31321&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=31321&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=31321&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=31321&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=31321&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=31321&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=31321&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=31321&r=support
Expected behavior: http://bugs.php.net/fix.php?id=31321&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=31321&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=31321&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=31321&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=31321&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=31321&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=31321&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=31321&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=31321&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=31321&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=31321&r=mysqlcfg