object pass by reference and call_user_func problems
| From: | Charles Killian | Date: | Tue, 11 Jul 2000 16:37:56 +0000 |
| Subject: | object pass by reference and call_user_func problems | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-5944@lists.php.net to get a copy of this message | ||
The problem is the $object properties don't get modified.
I do the following:
1. in a function I create a new $object
2. in the same function I pass the $object to another function (func2) using call_usr_func. (func2
is actually a property of $obj that I set so it needs to be called by call_usr_func)
3. func2 accepts this $object by reference
4. func2 calls one of $objects methods that modifies the $object's properties
5. I exit out of func2 and print the $object properties but they haven't changed
<?
class Beetle{
var $color;
}
function func1(){
$obj = new Beetle();
call_user_func("func2", $obj);// if I get rid of call_user_func, or pass &$obj it
works
print "$obj->color";// print nothing
}
function func2(&$obj){
$obj->color = "black";
}
func1();
?>
As stated in the comment if I don't use call_user_func() (not an option) it works. Or if I pass
by reference &$obj (deprecated) it works.
Does anybody know what's going on?
Charles