学习参考:http://cn.php.net/manual/en/book.reflection.php
反射API,包含一系列的类、异常和接口。可用来检测分析其他的类、接口、方法、属性、函数和扩展。
Reflector ,是其它反射类的接口类。Reflection 反射类ReflectionExtension 分析报告扩展类有关扩展ReflectionClass类: 分析报告类有关扩展ReflectionFunction 分析报告函数有关扩展ReflectionFunctionAbstract: A parent class to ReflectionFunction, read its description for details.ReflectionMethod 类报告有关方法的信息。ReflectionObject 类报告有关对象的信息。ReflectionParameter类检索一个函数或方法的参数信息。ReflectionProperty类报告一个类的属性信息。 ============
eg:
<?phpclass userClass {public function userMethod($userParameter='default') {}}foreach(get_declared_classes() as $class) {$reflectionClass = new ReflectionClass($class);if($reflectionClass->isUserDefined()) {Reflection::export($reflectionClass);}}
============ Reflector ,是其它反射类的接口类。
Reflector {/* 方法 */abstract public static string export ( void )abstract public string __toString ( void )} Reflection
Reflection {//导出一个类或方法的详细信息public static void export ( Reflector $reflector [, bool $return = false ] )//取得修饰符的名字public static array getModifierNames ( int $modifiers )}ReflectionExtension 分析报告扩展类有关扩展
ReflectionExtension implements Reflector {/* 属性 */public $ReflectionExtension->name ; //扩展名称/* 方法 */final private void ReflectionExtension::__clone ( void ) // 克隆ReflectionExtension::__construct ( string $name ) //public static string ReflectionExtension::export ( string $name [, string $return = false ] )public array ReflectionExtension::getClasses ( void ) //获取类public array ReflectionExtension::getClassNames ( void ) //获取类名public array ReflectionExtension::getConstants ( void ) //获取常数public array ReflectionExtension::getDependencies ( void ) //获取的依赖public array ReflectionExtension::getFunctions ( void ) //获取扩展函数public array ReflectionExtension::getINIEntries ( void ) //获取扩展INI条目public string ReflectionExtension::getName ( void ) //获取扩展public string ReflectionExtension::getVersion ( void ) //获取扩展的版本public string ReflectionExtension::info ( void ) //获取扩展信息public void ReflectionExtension::isPersistent ( void ) //判断这个类是否是是持久性的public void ReflectionExtension::isTemporary ( void ) //判断这个类是否是暂时的public string ReflectionExtension::__toString ( void ) //}ReflectionClass类: 分析报告类有关扩展
class ReflectionClass implements Reflector{final private __clone()public object __construct(string name)public string __toString()public static string export() //导出该类的详细信息public string getName() //取得类名或接口名public bool isInternal() //测试该类是否为系统内部类public bool isUserDefined() //测试该类是否为用户自定义类public bool isInstantiable() //测试该类是否被实例化过public bool hasConstant(string name) //测试该类是否有特定的常量public bool hasMethod(string name) //测试该类是否有特定的方法public bool hasProperty(string name) //测试该类是否有特定的属性public string getFileName() //取得定义该类的文件名,包括路径名public int getStartLine() //取得定义该类的开始行public int getEndLine() //取得定义该类的结束行public string getDocComment() //取得该类的注释public ReflectionMethod getConstructor() //取得该类的构造函数信息public ReflectionMethod getMethod(string name) //取得该类的某个特定的方法信息public ReflectionMethod[] getMethods() //取得该类的所有的方法信息public ReflectionProperty getProperty(string name) //取得某个特定的属性信息public ReflectionProperty[] getProperties() //取得该类的所有属性信息public array getConstants() //取得该类所有常量信息public mixed getConstant(string name) //取得该类特定常量信息public ReflectionClass[] getInterfaces() //取得接口类信息public bool isInterface() //测试该类是否为接口public bool isAbstract() //测试该类是否为抽象类public bool isFinal() //测试该类是否声明为final//取得该类的修饰符,返回值类型可能是个资源类型//通过Reflection::getModifierNames($class->getModifiers())进一步读取public int getModifiers()public bool isInstance(stdclass object) //测试传入的对象是否为该类的一个实例public stdclass newInstance(mixed* args) //创建该类实例public ReflectionClass getParentClass() //取得父类public bool isSubclassOf(ReflectionClass class) //测试传入的类是否为该类的父类public array getStaticProperties() //取得该类的所有静态属性public mixed getStaticPropertyValue(string name [, mixed default]) //取得该类的静态属性值,若private,则不可访问public void setStaticPropertyValue(string name, mixed value) //设置该类的静态属性值,若private,则不可访问,有悖封装原则public array getDefaultProperties() //取得该类的属性信息,不含静态属性public bool isIterateable()public bool implementsInterface(string name) //测试是否实现了某个特定接口public ReflectionExtension getExtension()public string getExtensionName()} ReflectionFunction 分析报告函数有关扩展
ReflectionFunction extends ReflectionFunctionAbstract implements Reflector {}ReflectionFunction::__construct — Constructs a ReflectionFunction objectReflectionFunction::export — 导出一个函数的详细信息ReflectionFunction::getClosure — 返回的函数动态创建的封闭ReflectionFunction::invoke — 调用功能ReflectionFunction::invokeArgs — 调用函数ARGSReflectionFunction::isDisabled — 检查,如果功能被禁用 ReflectionFunctionAbstract: A parent class to ReflectionFunction, read its description for details.
ReflectionFunctionAbstract implements Reflector {} ReflectionMethod 类报告有关方法的信息。
ReflectionMethod extends ReflectionFunctionAbstract implements Reflector {}ReflectionMethod::__construct — Constructs a ReflectionMethodReflectionMethod::export — 导出一个反射方法 Export a reflection method .ReflectionMethod::getClosure — 返回动态创建的方法封闭 Returns a dynamically created closure for the methodReflectionMethod::getDeclaringClass — 获取声明类的反射的方法 Gets declaring class for the reflected method.ReflectionMethod::getModifiers — 获取方法的修饰符 Gets the method modifiersReflectionMethod::getPrototype — 获取的方法原型(如果有的话) 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 privateReflectionMethod::isProtected — Checks if method is protectedReflectionMethod::isPublic — Checks if method is publicReflectionMethod::isStatic — Checks if method is staticReflectionMethod::setAccessible — Set method accessibilityReflectionMethod::__toString — Returns the string representation of the Reflection method object. ReflectionObject 类报告有关对象的信息。
ReflectionObject extends ReflectionClass implements Reflector {} ReflectionParameter类检索一个函数或方法的参数信息。
ReflectionParameter implements Reflector {}ReflectionProperty类报告一个类的属性信息。
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 itemsreturn array(array('description'=>'MyCoolPlugin','link'=>'/MyCoolPlugin'));}public static function getArticles() {//Numeric array of articlesreturn 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);