设为首页 收藏本站
查看: 377|回复: 0

[经验分享] PHP 反射,学习

[复制链接]

尚未签到

发表于 2017-3-21 11:52:27 | 显示全部楼层 |阅读模式
  学习参考: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);

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-353023-1-1.html 上篇帖子: php Cache 类 下篇帖子: php图像处理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表