开发者

PHP method_exists not working on class children?

开发者 https://www.devze.com 2023-02-25 08:01 出处:网络
class parent{ function run($methodname) { echo method_exists(__CLASS__, $methodname); } } class child extends parent {
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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消