|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-10-22 18:16 UTC] diego at ditech dot com dot br
Description:
------------
When trying to reassign an object inside a function, it is not acting as passed by reference.
Reproduce code:
---------------
<?php
class foo {
}
function change($obj) {
$obj = null;
}
function change_ref(&$obj) {
$obj = null;
}
$o = new foo;
change($o);
var_dump($o);
change_ref($o);
var_dump($o);
Expected result:
----------------
Both should yield the same result.
1st: NULL
2nd: NULL
Actual result:
--------------
Only change_ref is redefining the object.
1st:
object(foo)#1 (0) {
}
2nd: NULL
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 18 13:00:02 2025 UTC |
By the way, it is possible to change object attributes as if both were passed by reference. class foo { public $bar; } function change($obj) { $obj->bar = 1; $obj = null; } $o = new foo; change($o); var_dump($o); The result of this is: object(foo)#1 (1) { ["bar"] => int(1) }