Chapter 14. Reflection API (Zend Engine 2 only)

Table of Contents
Introduction
ReflectionFunction
ReflectionParameter
ReflectionClass
ReflectionMethod
ReflectionProperty
ReflectionExtension
Extending the reflection classes

Introduction

PHP5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions and methods as well as extensions. Additionally, the reflection API also offers ways of retrieving doc comments for functions, classes and methods.

The reflection API is an object-oriented extension to the Zend Engine, consisting of the following classes:

<?php
  class Reflection { }
  interface Reflector { }
  class ReflectionException extends Exception { }
  class ReflectionFunction implements Reflector { }
  class ReflectionParameter implements Reflector { }
  class ReflectionMethod extends ReflectionFunction { }
  class ReflectionClass implements Reflector { }
  class ReflectionProperty implements Reflector { }
  class ReflectionExtension implements Reflector { }
?>

Note: For details on these classes, have a look at the following chapters.

If we were to execute the code in the example below:

Example 14-1. Basic usage of the reflection API

<?php
  Reflection::export(new ReflectionClass('Exception'));
?>
We will see:
Class [ <internal> class exception ] {

  - Constants [0] {
  }

  - Properties [5] {
    Property [ <default> protected $message ]
    Property [ <default> protected $code ]
    Property [ <default> protected $file ]
    Property [ <default> protected $line ]
    Property [ <default> protected $trace ]
  }

  - Methods [6] {
    Method [ <internal> <ctor> public method __construct ] {
    }

    Method [ <internal> public method &getmessage ] {
    }

    Method [ <internal> final public method &getcode ] {
    }

    Method [ <internal> final public method &getfile ] {
    }

    Method [ <internal> final public method &getline ] {
    }

    Method [ <internal> final public method &gettrace ] {
    }

  }
}