note 98221 added to function.call-user-func-array
| From: | dot) | Date: | Wed, 02 Jun 2010 15:38:42 +0000 |
| Subject: | note 98221 added to function.call-user-func-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169821@lists.php.net to get a copy of this message | ||
I found an easy way to call a parent method in PHP < 5.3 without entering the method name
directly:
<?php
class MyClass extends AnotherClass {
function _myOverrideMethod() {
call_user_func(array('parent',__FUNCTION__));
}
}
?>
This will call the parent method of the current class (AnotherClass::_myOverrideMethod).
I also tried replacing __FUNCTION__ with __METHOD__ but that DOES NOT WORK. I var_dump'ed
__METHOD__ and realied it included the class name. So, for example, calling var_dump(__METHOD__)
within the class above will output 'MyClass::_myOverrideMethod' which will not work when
used as a replacement for __FUNCTION__ above because the call_user_func then tries to call
'parent::MyClass::_myOverrideMethod' which DOES NOT WORK. __FUNCTION__ included only the
method name (without the class). To compare: here are the outputs side-by-side:
<?php
// Supposing they were called within the MyClass::_myOverrideMethod method above...
var_dump(__METHOD__); // MyClass::_myOverrideMethod
var_dump(__FUNCTION__); // _myOverrideMethod
?>
----
Server IP: 69.147.83.197
Probable Submitter: 150.134.171.76
----
Manual Page -- http://www.php.net/manual/en/function.call-user-func-array.php
Edit -- https://master.php.net/note/edit/98221
Del: integrated -- https://master.php.net/note/delete/98221/integrated
Del: useless -- https://master.php.net/note/delete/98221/useless
Del: bad code -- https://master.php.net/note/delete/98221/bad+code
Del: spam -- https://master.php.net/note/delete/98221/spam
Del: non-english -- https://master.php.net/note/delete/98221/non-english
Del: in docs -- https://master.php.net/note/delete/98221/in+docs
Del: other reasons-- https://master.php.net/note/delete/98221
Reject -- https://master.php.net/note/reject/98221
Search -- https://master.php.net/manage/user-notes.php