ID: 31321 User updated by: i25506 at informatik dot fh-wuerzburg dot de Reported By: i25506 at informatik dot fh-wuerzburg dot de Status: Bogus Bug Type: *General Issues Operating System: Win32 and Linux PHP Version: 5.0.1 New Comment:
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... Previous Comments: ------------------------------------------------------------------------ [2004-12-28 15:25:43] [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 Also read the migration guide. ------------------------------------------------------------------------ [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