• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP Reflection API详解

php 搞代码 4年前 (2022-01-01) 46次浏览 已收录 0个评论

这篇文章主要介绍了PHP Reflection API详解,本文讲解了Reflection类、ReflectionException类、ReflectionFunction类、ReflectionParameter类、ReflectionClass类、ReflectionMethod类等内容,需要的朋友可以参考下

PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。

PHP Reflection API有:

 class Reflection { } interface Reflector { } class ReflectionException extends Exception { } class ReflectionFunction implements Reflector { } class ReflectionParameter implements Reflector { } class ReflectionMethod extends ReflectionFunction { } class ReflectionClass implements Reflector { } class ReflectionObject extends ReflectionClass { } class ReflectionProperty implements Reflector { } class ReflectionExtension implements Reflector { } 

具体API说明:

①Reflection类

 

②ReflectionException类

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

③ReflectionFunction类

  

④ReflectionParameter类:

  

⑤ReflectionClass类:

 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,则<div style="color:transparent">来源gaodai.ma#com搞#代!码网</div>不可访问,有悖封装原则 public array getDefaultProperties() //取得该类的属性信息,不含静态属性 public bool isIterateable() public bool implementsInterface(string name) //测试是否实现了某个特定接口 public ReflectionExtension getExtension() public string getExtensionName() } ?> 

⑥ReflectionMethod类:

  

⑦ReflectionProperty类:

  

⑧ReflectionExtension类

  

使用例子:

 sex = "male"; } public function action(){ echo "来自https://www.gaodaima.com的测试"; } } $class = new ReflectionClass('Person'); //获取属性 foreach($class->getProperties() as $property) { echo $property->getName()."\n"; } //获取方法 print_r($class->getMethods()); $p1 = new Person(); $obj = new ReflectionObject($p1); //获取对象和类的属性 print_r($obj->getProperties()); 

很明显上面代码中对象和类获取的属性是不同的,这是因为对象进行了contruct实例化,因此多了sex属性,PHP Reflection确实能够获取很多有用的信息。

以上就是PHP Reflection API详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP Reflection API详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址