1

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()
   }

}

I am not able to understand what is difference between these two calls

$publishMethod->invoke($editor);

and

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

I mean if we already was $editor then why would be invoke method through reflection class

zzlalani
  • 22,960
  • 16
  • 44
  • 73
fdsgds
  • 129
  • 6

2 Answers2

0

There is two approaches: call method directly and call method using reflection.

sectus
  • 15,605
  • 5
  • 55
  • 97
0

Sometimes you need to convert function name as "string" to a real callable function.

For example, with some validation framework, you need to configure some rules. e.g.

addRule("notEmpty") 

"notEmpty" will be a method name(as string), and you can invoke the real method by Reflection.

Andrew
  • 5,290
  • 1
  • 19
  • 22