首页 / PHP / 简述PHP反射五种类的使用
简述PHP反射五种类的使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了简述PHP反射五种类的使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3513字,纯文字阅读大概需要6分钟。
内容图文
![简述PHP反射五种类的使用](/upload/InfoBanner/zyjiaocheng/134/918a47d175d44f7c9950ed2d5090fa74.jpg)
PHP Reflection是用于获取类、扩展、方法、函数、对象、参数、属性的详细信息。
ReflectionClass类获取类相关信息,如获取属性、方法、文档注释等。
<?php class Person { /** * For the sake of demonstration, we"re setting this private */ private $_allowDynamicAttributes = false; /** type=primary_autoincrement */ protected $id = 0; /** type=varchar length=255 null */ protected $name; /** type=text null */ protected $biography; public function getId() { return $this->id; } public function setId($v) { $this->id = $v; } public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } public function getBiography() { return $this->biography; } public function setBiography($v) { $this->biography = $v; } } //导出类 ReflectionClass::export('Person'); $r = new ReflectionClass('Person'); //获取所有属性 print_r($r->getProperties()); /** * 获取指定属性 * ReflectionProperty::IS_STATIC * ReflectionProperty::IS_PUBLIC * ReflectionProperty::IS_PROTECTED * ReflectionProperty::IS_PRIVATE */ print_r($r->getProperties(ReflectionProperty::IS_PRIVATE)); //获取注释 print_r($r->getProperty('id')->getDocComment()); //获取方法 print_r($r->getMethods());
ReflectionExtension 类用于获取扩展相关信息
$re = new ReflectionExtension('Reflection'); print_r($re->getClasses()); //扩展的所有类 print_r($re->getClassNames()); //扩展所有类名 $dom = new ReflectionExtension('mysql'); print_r($dom->getConstants());//扩展常量 print_r($dom->getDependencies());//该扩展依赖 print_r($dom->getFunctions());//扩展方法 print_r($dom->getINIEntries());//扩展ini信息 print_r($dom->getName());//扩展名称 print_r($dom->getVersion());//扩展版本 print_r($dom->info());//扩展信息 print_r($dom->isPersistent());//是否是持久扩展 print_r($dom->isTemporary()); //是否是临时扩展
ReflectionFunction类 用户获取函数相关信息
$rf = new ReflectionFunction('array_merge'); foreach($rf->getParameters() as $item) { echo $item . PHP_EOL; }
ReflectionMethod类用户获取方法相关信息
class Person { public $name; /** * get name of person */ public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } } $rm = new ReflectionMethod('Person', 'getName'); print_r($rm->isPublic()); print_r($rm->getDocComment());
ReflectionObject 类 用于获取对象相关信息
class Person { public $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } } $a = new Person('a'); $ro = new ReflectionObject($a); print_r($ro->getMethods());
ReflectionParameter 获取函数或方法参数的相关信息。
class Person { public $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } } $p = new ReflectionParameter(array('Person', 'setName'), 0); print_r($p->getPosition()); //0 print_r($p->getName()); //v
ReflectionProperty 获取类的属性的相关信息。
class Person { /** 测试 */ public $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } } $p = new ReflectionProperty('Person', 'name'); print_r($p->getDocComment());
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
php读取、正则匹配邮件内容的方法
php实现持久层的方法
PHP实现简单的GET、POST、Cookie、Session等功能
以上就是简述PHP 反射五种类的使用的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的简述PHP反射五种类的使用全部内容,希望文章能够帮你解决简述PHP反射五种类的使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。