【问题标题】:Detect class name of method by name of extended class and method using php?使用php通过扩展类和方法的名称检测方法的类名?
【发布时间】:2018-11-24 10:43:31
【问题描述】:

我想知道 PHP 中的类方法在哪里。我定义了Animal 类和run 方法。我定义Dog 类扩展Animal 类。

$dog = new Dog();
echo where_is_method($dog->run); // -> Animal:run

我想从where_is_method 函数中获取输出。

【问题讨论】:

    标签: php class object


    【解决方案1】:

    您需要使用ReflectionMethod 类来查找方法的类名。

    class Animal {
        function run() {
            return 'run';
        }
    }
    class Dog extends Animal {
        function other(){}
    }
    $dog = new Dog();
    
    $reflection = new ReflectionMethod($dog, 'run');
    $className = $reflection->class;
    // Animal
    

    检查结果在demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多