note 21646 deleted from function.call-user-func by cmb
| From: | cmb@php.net | Date: | Sun, 08 Dec 2019 11:12:00 +0000 |
| Subject: | note 21646 deleted from function.call-user-func by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213443@lists.php.net to get a copy of this message | ||
Note Submitter: phil at gettcomm dot com
----
if you need to get a reference back from a method, you can work around call_user_func()'s
shortcomings like this:
<?php
$globalObj;
class tClass
{
function &instance()
{
global $globalObj;
if(!is_object($globalObj))
{
$globalObj = new tClass();
}
return $globalObj;
}
}
$classname = "tClass";
# calling the function this way won't
# return a reference.
$test = &call_user_func(array($classname,"instance"));
# but if we call it again with the instance
# that we just got, it'll be the right
# one
$test = &$test->instance();
# so test is now a reference to $globalObj, just like it should be
# let's verify it:
$test->blah=1;
echo "<pre>";
print_r($test)."\n";
print_r($globalObj);
echo "</pre>";
# there, now it behaves just like you'd expect
?>