note 48200 deleted from function.call-user-func by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 10:13:33 +0000 |
| Subject: | note 48200 deleted from function.call-user-func by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196911@lists.php.net to get a copy of this message | ||
Note Submitter: bostjan dot skufca at domenca dot com
----
This is the CORRECTED VERSION of previous note. After playing a while I realised that objects are
not passed by reference implicitly, you have to do it on your own, so:
call_user_func(array($objectBar, 'methodFoo')); // case sensitive
first duplicates object $objectBar (internally) and only then calls it's methodFoo. This kind
of use is discouraged as it may result in "unpredicted behaviour".
Correct version is here.
call_user_func(array(&$objectBar, 'methodFoo')); // case sensitive
(note the reference operator).
--- and the corrected note is here ---
I couldn't find any simple list of available callback variations, so I made one. It is tested
on PHP 4.3.9 only.
If you need to call ordinary function:
call_user_func('funcFoo');
If you need to call CLASS method (NOT object):
call_user_func(array('classBar', 'methodFoo')); // case insensitive
If you need to call OBJECT method:
call_user_func(array(&$objectBar, 'methodFoo')); // case sensitive
If you need to call method of object of object:
call_user_func(array(&$objectBar->objectTor, 'methodFoo')); // case sensitive
If you need to call object method from within the very same object (NOT CLASS!):
call_user_func(array(&$this, 'methodFoo'));