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

[经验分享] PHP 常用的反射函数

[复制链接]

尚未签到

发表于 2017-3-25 07:16:26 | 显示全部楼层 |阅读模式
1. get_class — 返回对象的类名
string get_class ([ object $obj ] )
返回对象实例 obj 所属类的名字。如果 obj 不是一个对象则返回 FALSE
Note: 在 PHP 扩展库中定义的类返回其原始定义的名字。在 PHP 4 中 get_class() 返回用户定义的类名的小写形式,但是在 PHP 5 中将返回类名定义时的名字,如同扩展库中的类名一样
 
 

[php] view plaincopy







  • //Note: 自 PHP 5 起,如果在对象的方法中调用则 obj 为可选项。  
  •   
  • //Example#1 使用 get_class()  
  •   
  • <?php  
  •   
  • class foo {  
  •     function foo()  
  •     {  
  •     // implements some logic  
  •     }  
  •   
  •     function name()  
  •     {  
  •         echo "My name is " , get_class($this) , "/n";  
  •     }  
  • }  
  •   
  • // create an object  
  • $bar = new foo();  
  •   
  • // external call  
  • echo "Its name is " , get_class($bar) , "/n";  
  •   
  • // internal call  
  • $bar->name();  
  •   
  • ?>   


 
 
 
  上例将输出:



Its name is foo
My name is foo
 

 

2.get_class_methods — 返回由类的方法名组成的数组

说明


array get_class_methods ( mixed $class_name )

返回由 class_name 指定的类中定义的方法名所组成的数组。如果出错,则返回 NULL
 
  Example#1 get_class_methods() 示例

[php] view plaincopy







  • <?php  
  • class myclass   
  • {  
  • // constructor      
  • function myclass()  
  • {  
  •     return(true);      
  • }         
  •  // method 1      
  • function myfunc1()  
  • {          
  •      return(true);      
  • }      
  • // method 2      
  • function myfunc2()      
  • {          
  •      return(true);      
  • }  
  • }  
  • $class_methods = get_class_methods('myclass');  
  •   
  • $class_methods = get_class_methods(new myclass());  
  •   
  • foreach ($class_methods as $method_name) {    echo "$method_name/n";  
  • }  
  • ?>   


  上例将输出:



myclass
myfunc1
myfunc2



get_class_vars — 返回由类的默认属性组成的数组


说明


array get_class_vars ( string $class_name )

返回由类的默认公有属性组成的关联数组,此数组的元素以 varname => value 的形式存在。
 
 
 

[php] view plaincopy







  • <?php  
  • class myclass {      
  • var $var1// 此变量没有默认值……      
  • var $var2 = "xyz";      
  • var $var3 = 100;      
  • private $var4// PHP 5      
  • // constructor      
  • function myclass() {          
  • // change some properties          
  • $this->var1 = "foo";          
  • $this->var2 = "bar";          
  • return true;      
  • }  
  • }  
  •   
  • $my_class = new myclass();  
  • $class_vars = get_class_vars(get_class($my_class));  
  • foreach ($class_vars as $name => $value)   
  • {      
  • echo "$name : $value/n";  
  • }  
  •   
  • ?>   



上例输出:




// 在 PHP 4.2.0 之前
var2 : xyz
var3 : 100
// 从 PHP 4.2.0 开始
var1 :
var2 : xyz
var3 : 100



[php] view plaincopy







  • class Memd extends Memcached {   
  •     /**  
  •      * 获取Memcached内置属性与函数  
  •      * @param int $flag default 1  
  •      */   
  •     public function getMemcachedVar($flag = 1) {   
  •         $r = new ReflectionClass($this);   
  •         switch($flag) {   
  •             case 1:   
  •                 return $r->getConstants();   
  •             case 2:   
  •                 return $r->getMethods();   
  •         }   
  •     }   
  • }   
  •   
  • $cls = new Memd();   
  • $info = $cls->getMemcachedVar(1);   
  • print_r($info);  



①Reflection类
[php] view plaincopy







  • class Reflection  
  • {  
  • public static mixed export(Reflector r [,bool return])  
  • //导出一个类或方法的详细信息  
  • public static array getModifierNames(int modifiers)  
  • //取得修饰符的名字  
  • }  


  ②ReflectionException类
  该类继承标准类,没特殊方法和属性。
  ③ReflectionFunction类

[php] view plaincopy







  • class ReflectionFunction 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 string getFileName()  
  • //取得文件名,包括路径名  
  • public int getStartLine()  
  • //取得定义函数的起始行  
  • public int getEndLine()  
  • //取得定义函数的结束行  
  • public string getDocComment()  
  • //取得函数的注释  
  • public array getStaticVariables()  
  • //取得静态变量  
  • public mixed invoke(mixed* args)  
  • //调用该函数,通过参数列表传参数  
  • public mixed invokeArgs(array args)  
  • //调用该函数,通过数组传参数  
  • public bool returnsReference()  
  • //测试该函数是否返回引用  
  • public ReflectionParameter[] getParameters()  
  • //取得该方法所需的参数,返回值为对象数组  
  • public int getNumberOfParameters()  
  • //取得该方法所需的参数个数  
  • public int getNumberOfRequiredParameters()  
  • //取得该方法所需的参数个数  
  • }  


  ④ReflectionParameter类:

[php] view plaincopy







  • class ReflectionParameter implements Reflector  
  • {  
  • final private __clone()  
  • public object __construct(string name)  
  • public string __toString()  
  • public static string export()  
  • //导出该参数的详细信息  
  • public string getName()  
  • //取得参数名  
  • public bool isPassedByReference()  
  • //测试该参数是否通过引用传递参数  
  • public ReflectionClass getClass()  
  • //若该参数为对象,返回该对象的类名  
  • public bool isArray()  
  • //测试该参数是否为数组类型  
  • public bool allowsNull()  
  • //测试该参数是否允许为空  
  • public bool isOptional()  
  • //测试该参数是否为可选的,当有默认参数时可选  
  • public bool isDefaultValueAvailable()  
  • //测试该参数是否为默认参数  
  • public mixed getDefaultValue()  
  • //取得该参数的默认值  
  • }  


  ⑤ReflectionClass类:

[php] view plaincopy







  • 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  
  • public int getModifiers()  
  • //取得该类的修饰符,返回值类型可能是个资源类型  
  • //通过Reflection::getModifierNames($class->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()  
  • }  


  ⑥ReflectionMethod类:

[php] view plaincopy







  • class ReflectionMethod extends ReflectionFunction  
  • {  
  • public __construct(mixed class, string name)  
  • public string __toString()  
  • public static string export()  
  • //导出该方法的信息  
  • public mixed invoke(stdclass object, mixed* args)  
  • //调用该方法  
  • public mixed invokeArgs(stdclass object, array args)  
  • //调用该方法,传多参数  
  • public bool isFinal()  
  • //测试该方法是否为final  
  • public bool isAbstract()  
  • //测试该方法是否为abstract  
  • public bool isPublic()  
  • //测试该方法是否为public  
  • public bool isPrivate()  
  • //测试该方法是否为private  
  • public bool isProtected()  
  • //测试该方法是否为protected  
  • public bool isStatic()  
  • //测试该方法是否为static  
  • public bool isConstructor()  
  • //测试该方法是否为构造函数  
  • public bool isDestructor()  
  • //测试该方法是否为析构函数  
  • public int getModifiers()  
  • //取得该方法的修饰符  
  • public ReflectionClass getDeclaringClass()  
  • //取得该方法所属的类  
  • // Inherited from ReflectionFunction  
  • final private __clone()  
  • public string getName()  
  • public bool isInternal()  
  • public bool isUserDefined()  
  • public string getFileName()  
  • public int getStartLine()  
  • public int getEndLine()  
  • public string getDocComment()  
  • public array getStaticVariables()  
  • public bool returnsReference()  
  • public ReflectionParameter[] getParameters()  
  • public int getNumberOfParameters()  
  • public int getNumberOfRequiredParameters()  
  • }  


  ⑦ReflectionProperty类:

[php] view plaincopy







  • class ReflectionProperty implements Reflector  
  • {  
  • final private __clone()  
  • public __construct(mixed class, string name)  
  • public string __toString()  
  • public static string export()  
  • //导出该属性的详细信息  
  • public string getName()  
  • //取得该属性名  
  • public bool isPublic()  
  • //测试该属性名是否为public  
  • public bool isPrivate()  
  • //测试该属性名是否为private  
  • public bool isProtected()  
  • //测试该属性名是否为protected  
  • public bool isStatic()  
  • //测试该属性名是否为static  
  • public bool isDefault()  
  • public int getModifiers()  
  • //取得修饰符  
  • public mixed getValue(stdclass object)  
  • //取得该属性值  
  • public void setValue(stdclass object, mixed value)  
  • //设置该属性值  
  • public ReflectionClass getDeclaringClass()  
  • //取得定义该属性的类  
  • public string getDocComment()  
  • //取得该属性的注释  
  • }  


  ⑧ReflectionExtension类

[php] view plaincopy







  • class ReflectionExtension implements Reflector {  
  • final private __clone()  
  • public __construct(string name)  
  • public string __toString()  
  • public static string export()  
  • //导出该扩展的所有信息  
  • public string getName()  
  • //取得该扩展的名字  
  • public string getVersion()  
  • //取得该扩展的版本  
  • public ReflectionFunction[] getFunctions()  
  • //取得该扩展的所有函数  
  • public array getConstants()  
  • //取得该扩展的所有常量  
  • public array getINIEntries()  
  • //取得与该扩展相关的,在php.ini中的指令信息  
  • public ReflectionClass[] getClasses()  
  • public array getClassNames()  
  • }  






  • __CLASS__ 获取当前类名(在此之前我用get_class - -!)
  • __FUNCTION__ 当前函数名(confirm)
  • __METHOD__ 当前方法名 (bankcard::confirm)

运维网声明 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-354889-1-1.html 上篇帖子: PHP面试题附答案 下篇帖子: php不好用之二
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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