php - 检查同一个类中是否存在方法

标签 php class methods exists

所以,method_exists() 需要一个对象来查看方法是否存在。但我想知道同一个类中是否存在方法。

我有一个方法可以处理一些信息并可以接收一个操作,它运行一个方法来进一步处理该信息。我想在调用它之前检查该方法是否存在。我怎样才能实现它?

例子:

class Foo{
    public function bar($info, $action = null){
        //Process Info
        $this->$action();
    }
}

最佳答案

你可以这样做:

class A{
    public function foo(){
        echo "foo";
    }

    public function bar(){
        if(method_exists($this, 'foo')){
            echo "method exists";
        }else{
            echo "method does not exist";
        }
    }
}

$obj = new A;
$obj->bar();

关于php - 检查同一个类中是否存在方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33938206/

相关文章:

python - 类变量的行为

methods - Go:方法调用后对象不持久

php - MySQL 连接并排除 count=0 的行

java - java中如何使用类

php - PHP 邮件中我们真的需要 -f 标志吗?为什么?

css - 在循环中的图像上设置一个类

file - Yii2 - 日志文件 (app.log) 以及如何访问它们?

python - 为什么 2.__add__(3) 在 Python 中不起作用?

php - CakePHP 分页 - 按虚拟字段排序

php - 在 Symfony 的 kernel.request 或 kernel.request 事件中获取当前用户