使用反射类调用方法和php中的正常方式有什么区别


What is difference between invoking method with reflection class and normal way in php

http://net.tutsplus.com/tutorials/php/reflection-in-php/

// Nettuts.php
class Nettuts {
   function publishNextArticle($editor) {
      $editor->setNextArticle('135523');
      $editor->publish(); // first call to publish()
      $reflector = new ReflectionClass($editor);
      $publishMethod = $reflector->getMethod('publish');
      $publishMethod->invoke($editor); // second call to publish()
   }
}

我无法理解这两个电话之间有什么区别

$publishMethod->invoke($editor);

$editor->publish(); // first call to publish()

我的意思是,如果我们已经$editor那么为什么要通过反射类调用方法

有两种方法:直接调用方法和使用反射调用方法。

有时您需要将函数名称作为"字符串"转换为真正的可调用函数。

例如,对于某些验证框架,您需要配置一些规则。

addRule("notEmpty") 

"notEmpty"将是一个方法名称(作为字符串),您可以通过反射调用真正的方法。