ReflectionMethod

The ReflectionMethod class lets you reverse-engineer class methods.

<?php
  class ReflectionMethod extends ReflectionFunction {
      public __construct(mixed class, string name)
      public mixed invoke(stdclass object, mixed* args)
      public bool isFinal()
      public bool isAbstract()
      public bool isPublic()
      public bool isPrivate()
      public bool isProtected()
      public bool isStatic()
      public bool isConstructor()
      public int getModifiers()
      public ReflectionClass getDeclaringClass()

      /* Inherited from ReflectionFunction */
      public string getName()
      public bool isInternal()
      public bool isUserDefined()
      public string getFileName()
      public int getStartLine()
      public int getEndLine()
      public string getDocComment()
      public array getStaticVariables()
      public string toString()
      public bool returnsReference()
      public ReflectionParameter[] getParameters()
  }
?>

To introspect a method, you will first have to create an instance of the ReflectionMethod class. You can then call any of the above methods on this instance.

Example 14-5. Using the ReflectionMethod class

<?php
  class Counter {
      private static $c= 0;

      /**
       * Increment counter
       *
       * @final
       * @static
       * @access  public
       * @return  int
       */
      final public static function increment()
      {
          self::$c++;
          return self::$c;
      }
  }

  // Create an instance of the ReflectionMethod class
  $method= new ReflectionMethod('Counter', 'increment');

  // Print out basic information
  printf(
    "===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n".
    "     declared in %s\n".
    "     lines %d to %d\n".
    "     having the modifiers %d[%s]\n",
    $method->isInternal() ? 'internal' : 'user-defined',
    $method->isAbstract() ? ' abstract' : '',
    $method->isFinal() ? ' final' : '',
    $method->isPublic() ? ' public' : '',
    $method->isPrivate() ? ' private' : '',
    $method->isProtected() ? ' protected' : '',
    $method->isStatic() ? ' static' : '',
    $method->getName(),
    $method->isConstructor() ? 'the constructor' : 'a regular method',
    $method->getFileName(),
    $method->getStartLine(),
    $method->getEndline(),
    $method->getModifiers(),
    implode(' ', Reflection::getModifierNames($method->getModifiers()))
  );

  // Print documentation comment
  printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1));

  // Print static variables if existant
  if ($statics= $method->getStaticVariables())
  {
      printf("---> Static variables: %s\n", var_export($statics, 1));
  }

  // Invoke the method
  printf("---> Invokation results in: ");
  var_dump($method->invoke(NULL));
?>

Note: Trying to invoke private, protected or abstract methods will result in an exception being thrown from the invoke() method.

Note: For static methods as seen above, you should pass NULL as the first argument to invoke(). For non-static methods, pass an instance of the class.