php教程

超轻量级php框架startmvc

php面向对象之反射功能与用法分析

更新时间:2020-03-18 18:37:20 作者:startmvc
本文实例讲述了php面向对象之反射功能与用法。分享给大家供大家参考,具体如下:个人对

本文实例讲述了php面向对象之反射功能与用法。分享给大家供大家参考,具体如下:

个人对反射定义的理解:

首先得说说什么叫反射。对于一个新手来说,反射这个概念常常给人一种似懂非懂的 感觉,不知道该如何下手操作。

反射是指:指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类、方法、属性、参数等的详细信息,同时也包括注释。这种动态获取的信息以及动态调用对象的方法 的功能称为反射API。反射是操纵面向对象范型中元模型的API,其功能十分强大,可帮助我们构建复杂,可扩展的应用。(注意:php中这种反向操作,实在PHP5之后才完全具备

下面在此我用实例进行说明:


class test{
 private $A;
 public $B;
 protected $C;
 public function test(){
 return "this is a test function";
 }
}
//实例化一个反射类ReflectionClass
$obj=new ReflectionClass('test');
echo $obj."<br>";
//实例化test类,并访问其test方法
$obj2=$obj->newInstance();
echo $obj2->test();

个人实例返回结果:


/** * xxx.php * ============================================== * Copy right 2012-2015 * ---------------------------------------------- * This is not a free software, without any authorization is not allowed to use and spread. * ============================================== * @Author:YeXianMing * @Email:LangWaiShiGe@hotmail.com * @Version:zend studio10.6.2 php5.4.38 apache2.2 */ Class [ class test ] { @@ D:\www\MyProjecttest\index5.php 13-21 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [3] { Property [ private $A ] Property [ public $B ] Property [ protected $C ] } - Methods [1] { Method [ public method test ] { @@ D:\www\MyProjecttest\index5.php 18 - 20 } } }
this is a test function

php 面向对象 反射