Zend_Reflection Examples

Zend_Reflection Reference

The various classes in Zend_Reflection mimic the API of PHP's » Reflection API - with one important difference. PHP's Reflection API does not provide introspection into docblock annotation tags, nor into parameter variable types or return types.

Zend_Reflection analyzes method docblock annotations to determine parameter variable types and the return type. Specifically, the @param and @return annotations are used. However, you can also check for any other annotation tags, as well as the standard "short" and "long" descriptions.

Each reflection object in Zend_Reflection overrides the getDocblock() method to return an instance of Zend_Reflection_Docblock. This class provides introspection into the docblocks and annotation tags.

Zend_Reflection_File is a new reflection class that allows introspection of PHP files. With it, you can retrieve the classes, functions, and global PHP code contained in the file.

Finally, the various methods that return other reflection objects allow a second parameter, the name of the reflection class to use for the returned reflection object.

Zend_Reflection_Docblock

Zend_Reflection_Docblock is the heart of Zend_Reflection's value-add over PHP's Reflection API. It provides the following methods:

  • getContents(): returns the full contents of the docblock.

  • getStartLine(): returns the starting position of the docblock within the defining file.

  • getEndLine(): get last line of docblock within the defining file.

  • getShortDescription(): get the short, one-line description (usually the first line of the docblock).

  • getLongDescription(): get the long description from the docblock.

  • hasTag($name): determine if the docblock has the given annotation tag.

  • getTag($name): Retrieve the given annotation tag reflection object, or a boolean FALSE if it's not present.

  • getTags($filter): Retrieve all tags, or all tags matching the given $filter string. The tags returned will be an array of Zend_Reflection_Docblock_Tag objects.

Zend_Reflection_Docblock_Tag

Zend_Reflection_Docblock_Tag provides reflection for individual annotation tags. Most tags consist of only a name and a description. In the case of some special tags, the class provides a factory method for retrieving an instance of the appropriate class.

The following methods are defined for Zend_Reflection_Docblock_Tag:

  • factory($tagDocblockLine): instantiate the appropriate tag reflection class and return it.

  • getName(): return the annotation tag name.

  • getDescription(): return the annotation description.

Zend_Reflection_Docblock_Tag_Param

Zend_Reflection_Docblock_Tag_Param is a specialized version of Zend_Reflection_Docblock_Tag. The @param annotation tag description consists of the parameter type, variable name, and variable description. It adds the following methods to Zend_Reflection_Docblock_Tag:

  • getType(): return the parameter variable type.

  • getVariableName(): return the parameter variable name.

Zend_Reflection_Docblock_Tag_Return

Like Zend_Reflection_Docblock_Tag_Param, Zend_Reflection_Docblock_Tag_Return is a specialized version of Zend_Reflection_Docblock_Tag. The @return annotation tag description consists of the return type and variable description. It adds the following method to Zend_Reflection_Docblock_Tag:

  • getType(): return the return type.

Zend_Reflection_File

Zend_Reflection_File provides introspection into PHP files. With it, you can introspect the classes, functions, and bare PHP code defined in a file. It defines the following methods:

  • getFileName(): retrieve the filename of the file being reflected.

  • getStartLine(): retrieve the starting line of the file (always "1").

  • getEndLine() retrieve the last line / number of lines in the file.

  • getDocComment($reflectionClass = 'Zend_Reflection_Docblock'): retrive the file-level docblock reflection object.

  • getClasses($reflectionClass = 'Zend_Reflection_Class'): retrieve an array of reflection objects, one for each class defined in the file.

  • getFunctions($reflectionClass = 'Zend_Reflection_Function'): retrieve an array of reflection objects, one for each function defined in the file.

  • getClass($name = null, $reflectionClass = 'Zend_Reflection_Class'): retrieve the reflection object for a single class.

  • getContents(): retrieve the full contents of the file.

Zend_Reflection_Class

Zend_Reflection_Class extends ReflectionClass, and follows its API. It adds one additional method, getDeclaringFile(), which may be used to retrieve the Zend_Reflection_File reflection object for the defining file.

Additionally, the following methods add an additional argument for specifying the reflection class to use when fetching a reflection object:

  • getDeclaringFile($reflectionClass = 'Zend_Reflection_File')

  • getDocblock($reflectionClass = 'Zend_Reflection_Docblock')

  • getInterfaces($reflectionClass = 'Zend_Reflection_Class')

  • getMethod($reflectionClass = 'Zend_Reflection_Method')

  • getMethods($filter = -1, $reflectionClass = 'Zend_Reflection_Method')

  • getParentClass($reflectionClass = 'Zend_Reflection_Class')

  • getProperty($name, $reflectionClass = 'Zend_Reflection_Property')

  • getProperties($filter = -1, $reflectionClass = 'Zend_Reflection_Property')

Zend_Reflection_Extension

Zend_Reflection_Extension extends ReflectionExtension, and follows its API. It overrides the following methods to add an additional argument for specifying the reflection class to use when fetching a reflection object:

  • getFunctions($reflectionClass = 'Zend_Reflection_Function'): retrieve an array of reflection objects representing the functions defined by the extension.

  • getClasses($reflectionClass = 'Zend_Reflection_Class'): retrieve an array of reflection objects representing the classes defined by the extension.

Zend_Reflection_Function

Zend_Reflection_Function adds a method for retrieving the function return type, as well as overrides several methods to allow specifying the reflection class to use for returned reflection objects.

  • getDocblock($reflectionClass = 'Zend_Reflection_Docblock'): retrieve the function docblock reflection object.

  • getParameters($reflectionClass = 'Zend_Reflection_Parameter'): retrieve an array of all function parameter reflection objects.

  • getReturn(): retrieve the return type reflection object.

Zend_Reflection_Method

Zend_Reflection_Method mirrors Zend_Reflection_Function, and only overrides one additional method:

  • getParentClass($reflectionClass = 'Zend_Reflection_Class'): retrieve the parent class reflection object.

Zend_Reflection_Parameter

Zend_Reflection_Parameter adds a method for retrieving the parameter type, as well as overrides methods to allow specifying the reflection class to use on returned reflection objects.

  • getDeclaringClass($reflectionClass = 'Zend_Reflection_Class'): get the declaring class of the parameter as a reflection object (if available).

  • getClass($reflectionClass = 'Zend_Reflection_Class'): get the class of the parameter as a reflection object (if available).

  • getDeclaringFunction($reflectionClass = 'Zend_Reflection_Function'): get the function of the parameter as a reflection object (if available).

  • getType(): get the parameter type.

Zend_Reflection_Property

Zend_Reflection_Property overrides a single method in order to allow specifying the returned reflection object class:

  • getDeclaringClass($reflectionClass = 'Zend_Reflection_Class'): retrieve the declaring class of the property as a reflection object.


Zend_Reflection Examples