note 98221 deleted from function.call-user-func-array by degeberg
| From: | degeberg@php.net | Date: | Wed, 02 Jun 2010 15:40:28 +0000 |
| Subject: | note 98221 deleted from function.call-user-func-array by degeberg | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169822@lists.php.net to get a copy of this message | ||
Note Submitter: jimbo2150 (at) gmail (dot) com
----
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
?>