繁体   English   中英

带有反射类的调用方法与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”将是一个方法名称(作为字符串),您可以通过反射调用真实方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM