导航首页 » 技术教程 » php面向对象之反射功能与用法分析
php面向对象之反射功能与用法分析 110 2024-01-10   

本文实例讲述了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:wwwMyProjecttestindex5.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:wwwMyProjecttestindex5.php 18 - 20 } } }
this is a test function

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。



!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103