PHP method_exists not working on class children?
class parent{
function run($methodname) {
echo method_exists(__CLASS__, $methodname);
}
}
class child extends parent {
function order(){
echo 'hello';
}
}
$test = new child();
$test->ru开发者_JAVA技巧n('order'); //false
The method_exists cannot find the method order in child class.
How to make it work?
__CLASS__
is bound to the class it's used in, not to inheriting classes. You can solve this by using $this
as the object reference.
Also see http://www.php.net/manual/en/language.oop5.late-static-bindings.php.
Try
echo method_exists($this, $methodname);
I used Reflection Class to get class details. Hope it helps
function ownMethodExist($class, $method)
{
$className = get_class($class);
$reflection = new ReflectionClass($className);
if($reflection->getMethod($method)->class == $className) {
return true;
}
return false;
}
精彩评论