Learning reference: http://cn.php.net/manual/en/book.reflection.php
Reflection APIs include a series of classes, exceptions, and interfaces. It can be used to detect and analyze other classes, interfaces, methods, attributes, functions, and extensions.
Reflector is an interface class for other reflection classes. Reflection class ReflectionExtension analysis report extension class about extension ReflectionClass class: Analysis Report class about extension ReflectionFunction Analysis Report function about extension ReflectionFunctionAbstract: A parent class to ReflectionFunction, read its description for details. the ReflectionMethod class reports information about methods. The ReflectionObject class reports information about objects. The ReflectionParameter class retrieves the parameter information of a function or method. The ReflectionProperty class reports the attributes of a class.
================
Eg:
<?phpclass userClass { public function userMethod($userParameter='default') {}}foreach(get_declared_classes() as $class) { $reflectionClass = new ReflectionClass($class); if($reflectionClass->isUserDefined()) { Reflection::export($reflectionClass); }}
================
ReflectorIs the interface class of other reflection classes.
Reflector {/* Method */abstract public static string export (void) abstract public string _ toString (void )}
Reflection
Reflection {// export the details of a class or method public static void export (Reflector $ reflector [, bool $ return = false]) // get the modifier name public static array getModifierNames (int $ modifiers )}
ReflectionextensionAnalysis Report extensions
ReflectionExtension implements Reflector {/* attribute */public $ ReflectionExtension-> name; // extension name/* Method */final private void ReflectionExtension :__ clone (void) // clone ReflectionExtension :__ construct (string $ name) // public static string ReflectionExtension: export (string $ name [, string $ return = false]) public array ReflectionExtension :: getClasses (void) // obtain the public array ReflectionExtension: getClassNames (void) // obtain the class name public array ReflectionExtension: getConstants (void) // obtain the constant public array ReflectionExtension :: getDependencies (void) // obtain the public array ReflectionExtension: getFunctions (void) // obtain the extension function public array ReflectionExtension: getINIEntries (void) // obtain the extended INI entry public string ReflectionExtension: getName (void) // obtain the extended public string ReflectionExtension: getVersion (void) // obtain the extended version public string ReflectionExtension :: info (void) // obtain the extension information public void ReflectionExtension: isPersistent (void) // determine whether the class is a persistent public void ReflectionExtension: isTemporary (void) // determine whether this class is a temporary public string ReflectionExtension ::__ toString (void )//}
ReflectionclassClass: Analysis Report class related extensions
Class ReflectionClass implements Reflector {final private _ clone () public object _ construct (string name) public string _ toString () public static string export () // export the detailed information of the class public string getName () // obtain the class name or interface name public bool isInternal () // test whether the class is a system internal class public bool isUserDefined () // test whether the class is a user-defined class public bool isInstantiable () // test whether the class has been instantiated public bool hasConstant (string name) // test whether the class has a specific constant public bool hasMethod (string name) // test whether the class has a specific method public bool hasProperty (string name) // test whether the class has a specific attribute public string getFileName () // obtain the name of the file defining the class, including the path name public int getStartLine () // obtain the start line public int getEndLine () for defining the class // obtain the end line public string getDocComment () // obtain the comment public ReflectionMethod getConstructor () for this class () // obtain the constructor information of this class public ReflectionMethod getMethod (string name) // obtain the information of a specific method of this class public ReflectionMethod [] getMethods () // obtain all the method information of this class public ReflectionProperty getProperty (string name) // obtain a specific property information public ReflectionProperty [] getProperties () // obtain all attributes of the class public array getConstants () // obtain all constants of the class public mixed getConstant (string name) // obtain the information of a specific constant of this class public ReflectionClass [] getInterfaces () // obtain the interface class information public bool isInterface () // test whether the class is an interface public bool isAbstract () // test whether the class is an abstract class public bool isFinal () // test whether the class is declared as final // get the modifier of the class. The returned type may be a resource type // use Reflection:: getModifierNames ($ class-> getModifiers () further reads public int getModifiers () public bool isInstance (stdclass object) // test whether the input object is an instance of the class public stdclass newInstance (mixed * args) // create the instance public ReflectionClass getParentClass () // obtain the parent class public bool isSubclassOf (ReflectionClass class) // test whether the passed class is the class's parent class public array getStaticProperties () // obtain all the static attributes of the class public mixed getStaticPropertyValue (string name [, mixed default]) // obtain the static attribute values of the class. If private, public void setStaticPropertyValue (string name, mixed value) is not accessible. // you can set the static attribute values of this class. If private, the static attribute values are not accessible. The encapsulation principle is invalid. public array getDefaultProperties () // obtain the attribute information of this class, excluding the static attribute public bool isIterateable () public bool implementsInterface (string name) // test whether a specific interface public ReflectionExtension getExtension () is implemented () public string getExtensionName ()}
ReflectionfunctionAnalysis Report function extensions
Reflectionfunction extends implements reflector {} reflectionfunction ::__ construct-constructs a reflectionfunction detail: Export-export details of a function reflectionfunction: getclosure-closed reflectionctionfunction dynamically created by the returned function:: invoke-call function reflectionfunction: invokeargs-call function argsreflectionfunction: isdisabled-check if the function is disabled
Reflectionfunctionabstract: A parent class to reflectionfunction, read its description for details.
ReflectionFunctionAbstract implements Reflector {}
ReflectionmethodClass report information about the method.
ReflectionMethod extends ReflectionFunctionAbstract implements Reflector {} ReflectionMethod ::__ construct-Constructs a ReflectionMethodReflectionMethod: export-Export a reflection method export a reflection method. reflectionMethod: getClosure-return the dynamically created method to close Returns a dynamically created closure for the methodReflectionMethod: getDeclaringClass-get the Reflection method of the Declaration class Gets declaring class for the reflected method. reflectionMethod: getModifiers-Get method modifier Gets the method modifiersReflectionMethod: getPrototype-Get method prototype (if any) Gets the method prototype (if there is one ). reflectionMethod: invoke-InvokeReflectionMethod: invokeArgs-Invoke argsReflectionMethod: isAbstract-Checks if method is abstractReflectionMethod: isConstructor-Checks if method is a constructorReflectionMethod :: isDestructor-Checks if method is a destructorReflectionMethod: isFinal-Checks if method is finalReflectionMethod: isPrivate-Checks if method is protected: isProtected-Checks if method is protectedReflectionMethod :: isPublic-Checks if method is publicReflectionMethod: isStatic-Checks if method is staticReflectionMethod: setAccessible-Set method failed :__ toString-Returns the string representation of the Reflection method object.
ReflectionobjectClass report information about objects.
ReflectionObject extends ReflectionClass implements Reflector {}
ReflectionparameterClass to retrieve the parameter information of a function or method.
ReflectionParameter implements Reflector {}
The reflectionproperty class reports the attributes of a class.
ReflectionProperty implements Reflector {}
Reflectionexception
ReflectionException extends Exception {/* Inherited methods */final public string Exception::getMessage ( void )final public Exception Exception::getPrevious ( void )final public mixed Exception::getCode ( void )final public string Exception::getFile ( void )final public int Exception::getLine ( void )final public array Exception::getTrace ( void )final public string Exception::getTraceAsString ( void )public string Exception::__toString ( void )final private void Exception::__clone ( void )}
Eg:
<?phpinterface IPlugin { public static function getName();}class MyCoolPlugin implements IPlugin { public static function getName() { return 'MyCoolPlugin'; } public static function getMenuItems() { //Numeric indexed array of menu items return array(array( 'description'=>'MyCoolPlugin', 'link'=>'/MyCoolPlugin' )); } public static function getArticles() { //Numeric array of articles return array(array( 'path'=>'/MyCoolPlugin', 'title'=>'This is a really cool article', 'text'=>'This article is cool because...' ) ); }}function findPlugins() { $plugins = array(); foreach(get_declared_classes() as $class) { $reflectionClass = new ReflectionClass($class); if($reflectionClass->implementsInterface('IPlugin')) { $plugins[] = $reflectionClass; } } return $plugins;}########################################function computeMenu() { $menu = array(); foreach(findPlugins() as $plugin) { if($plugin->hasMethod('getMenuItems')) { $reflectionMethod = $plugin->getMethod('getMenuItems'); if($reflectionMethod->isStatic()) { $items = $reflectionMethod->invoke(null); } else { //If the method isn't static we need an instance $pluginInstance = $plugin->newInstance(); $items = $reflectionMethod->invoke($pluginInstance); } $menu = array_merge($menu, $items); } } return $menu;}function computeArticles() { $articles = array(); foreach(findPlugins() as $plugin) { if($plugin->hasMethod('getArticles')) { $reflectionMethod = $plugin->getMethod('getArticles'); if($reflectionMethod->isStatic()) { $items = $reflectionMethod->invoke(null); } else { $pluginInstance = $plugin->newInstance(); $items = $reflectionMethod->invoke($pluginInstance); } $articles = array_merge($articles, $items); } } return $articles;}function computeSidebars() { $sidebars = array(); foreach(findPlugins() as $plugin) { if($plugin->hasMethod('getSidebars')) { $reflectionMethod = $plugin->getMethod('getSidebars'); if($reflectionMethod->isStatic()) { $items = $reflectionMethod->invoke(null); } else { $pluginInstance = $plugin->newInstance(); $items = $reflectionMethod->invoke($pluginInstance); } $sidebars = array_merge($sidebars, $items); } } return $sidebars;}########################################$menu = computeMenu();$sidebars = computeSidebars();$articles = computeArticles();//This could be a lot more complexprint_r($menu);print_r($sidebars);print_r($articles);