|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-12-28 15:25 UTC] derick@php.net
[2004-12-30 16:56 UTC] i25506 at informatik dot fh-wuerzburg dot de
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 06 20:00:02 2026 UTC |
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! )