加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP Reflection API详解

发布时间:2020-12-12 20:52:23 所属栏目:PHP教程 来源:网络整理
导读:PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。 PHP Reflection API有: 具体API说明: ①Reflection类 ②ReflectionException类 该类继承标准类,没特殊方法和属性。 ③ReflectionFunction

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

PHP Reflection API有:

具体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,则不可访问,有悖封装原则 public array getDefaultProperties() //取得该类的属性信息,不含静态属性 public bool isIterateable() public bool implementsInterface(string name) //测试是否实现了某个特定接口 public ReflectionExtension getExtension() public string getExtensionName() } ?>

⑥ReflectionMethod类:

⑦ReflectionProperty类:

⑧ReflectionExtension类

?>

使用例子:

public $age;

public function __construct(){
$this->sex = "male";
}

public function action(){
echo "来自//www.52php.cn的测试";
}
}

$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确实能够获取很多有用的信息。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读