`
huobengluantiao8
  • 浏览: 1077260 次
文章分类
社区版块
存档分类
最新评论

PHP reflection

 
阅读更多

PHP5添加了一项新的功能:Reflection。这个功能使得程序员可以reverse-engineer class, interface,function,method and extension。通过PHP代码,就可以得到某object的所有信息,并且可以和它交互。

假设有一个类Person:

01 classPerson {
02 /**
03 * For the sake of demonstration, we"re setting this private
04 */
05 private$_allowDynamicAttributes= false;
06
07 /** type=primary_autoincrement */
08 protected$id= 0;
09
10 /** type=varchar length=255 null */
11 protected$name;
12
13 /** type=text null */
14 protected$biography;
15
16 publicfunctiongetId()
17 {
18 return$this->id;
19 }
20 publicfunctionsetId($v)
21 {
22 $this->id =$v;
23 }
24 publicfunctiongetName()
25 {
26 return$this->name;
27 }
28 publicfunctionsetName($v)
29 {
30 $this->name =$v;
31 }
32 publicfunctiongetBiography()
33 {
34 return$this->biography;
35 }
36 publicfunctionsetBiography($v)
37 {
38 $this->biography =$v;
39 }
40 }

通过ReflectionClass,我们可以得到Person类的以下信息:

  • 常量 Contants
  • 属性 Property Names
  • 方法 Method Names
  • 静态属性 Static Properties
  • 命名空间 Namespace
  • Person类是否为final或者abstract

只要把类名"Person"传递给ReflectionClass就可以了:

1 $class=newReflectionClass('Person');

获取属性(Properties):

1 $properties=$class->getProperties();
2 foreach($propertiesas$property) {
3 echo$property->getName()."\n";
4 }
5 // 输出:
6 // _allowDynamicAttributes
7 // id
8 // name
9 // biography

默认情况下,ReflectionClass会获取到所有的属性,private 和 protected的也可以。如果只想获取到private属性,就要额外传个参数:

1 $private_properties=$class->getProperties(ReflectionProperty::IS_PRIVATE);

可用参数列表:

  • ReflectionProperty::IS_STATIC
  • ReflectionProperty::IS_PUBLIC
  • ReflectionProperty::IS_PROTECTED
  • ReflectionProperty::IS_PRIVATE

如果要同时获取public 和private 属性,就这样写:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED

应该不会感觉陌生吧。

通过$property->getName()可以得到属性名,通过getDocComment可以得到写给property的注释。

01 foreach($propertiesas$property) {
02 if($property->isProtected()) {
03 $docblock=$property->getDocComment();
04 preg_match('/ type\=([a-z_]*) /',$property->getDocComment(),$matches);
05 echo$matches[1]."\n";
06 }
07 }
08 // Output:
09 // primary_autoincrement
10 // varchar
11 // text

有点不可思议了吧。竟然连注释都可以取到。

获取方法(methods):通过getMethods() 来获取到类的所有methods。返回的是ReflectionMethod对象的数组。不再演示。

最后通过ReflectionMethod来调用类里面的method。

01 $data=array("id"=> 1,"name"=>"Chris","biography"=>"I am am a PHP developer");
02 foreach($dataas$key=>$value) {
03 if(!$class->hasProperty($key)) {
04 thrownewException($key." is not a valid property");
05 }
06
07 if(!$class->hasMethod("get".ucfirst($key))) {
08 thrownewException($key." is missing a getter");
09 }
10
11 if(!$class->hasMethod("set".ucfirst($key))) {
12 thrownewException($key." is missing a setter");
13 }
14
15 // Make a new object to interact with
16 $object=newPerson();
17
18 // Get the getter method and invoke it with the value in our data array
19 $setter=$class->getMethod("set".ucfirst($key));
20 $ok=$setter->invoke($object,$value);
21
22 // Get the setter method and invoke it
23 $setter=$class->getMethod("get".ucfirst($key));
24 $objValue=$setter->invoke($object);
25
26 // Now compare
27 if($value==$objValue) {
28 echo"Getter or Setter has modified the data.\n";
29 }else{
30 echo"Getter and Setter does not modify the data.\n";
31 }
32 }

有点意思吧。

分享到:
评论

相关推荐

    php-type-reflection:提供类以统一方式反映 php 类型的库

    安装您可以使用下载并安装 PHP Type Reflection。 要将 PHP 类型反射添加到您的项目中,只需将 hediet/type-reflection 依赖项添加到您的项目的composer.json文件中。 这是一个composer.json文件的最小示例, ...

    php-phpdocumentor-reflection-docblock5-5.1.0-1.el7.remi.noarch.rpm

    php-phpdocumentor-reflection-docblock5-5.1.0-1.el7.remi.noarch.rpm

    php-phpdocumentor-reflection-docblock5-5.2.0-1.el7.remi.noarch.rpm

    php-phpdocumentor-reflection-docblock5-5.2.0-1.el7.remi.noarch.rpm

    php-phpdocumentor-reflection-common2-2.2.0-1.el7.remi.noarch.rpm

    php-phpdocumentor-reflection-common2-2.2.0-1.el7.remi.noarch.rpm

    laminas-code:PHP Reflection API,静态代码扫描和代码生成的扩展

    椎板代码 Laminas\Code\Generator... 虽然当前的实现仅限于生成PHP代码,但您可以轻松扩展基类,以便为其他任务提供代码生成:JavaScript,配置文件,apache虚拟主机等。 文件问题位于 文档位于 从v2到v3的迁移文档位于

    Laravel开发-reflection

    在Laravel框架中,Reflection是一个重要的概念,它主要涉及到PHP的反射API(Reflection API)。反射是一种在运行时分析类、接口、方法等元数据的技术,它允许程序在执行过程中检查自身的行为和结构。在Laravel中,...

    B4A Reflection Library

    B4A Reflection Library

    php-reflection:基于php-parserPHP文件的Nodejs Reflection API

    基于PHP文件的Nodejs Reflection API 安装 npm install php-reflection --save 用法 var reflection = require ( 'php-reflection' ) ; var workspace = new reflection . Repository ( '/home/devbox/my-project' ...

    PHP DocAnnotation-开源

    PHP DocAnnotation 是一个针对PHP开发者的开源工具,它利用了PHP的DocComment和Reflection API来实现与Java注解相类似的功能。在PHP中,注释通常用于提供代码的元数据,而DocAnnotation框架正是以此为基础,扩展了...

    php-reflection-code:PHP反射IDE自动提示生成器

    PHP反射代码 PHP反射IDE自动提示生成器 描述 本程序在命令行(CLI)中执行,通过PHP反射将内置类生成一...php php-reflection-code.php $extension_class_name 示例: php php-reflection-code.php redis 定义批量执行

    实例介绍PHP的Reflection反射机制

    PHP的Reflection(反射)机制是PHP5新增的一项功能,它允许程序员在运行时对类、接口、函数、方法和扩展进行反向工程。通过Reflection API,PHP代码可以获取到关于对象的所有信息,并且能够与对象进行交互。例如,...

    PHP通过反射动态加载第三方类和获得类源码的实例

    在PHP编程中,反射是一种强大的工具,它允许我们在运行时检查和操作类、对象、接口、方法等。本文将深入探讨如何使用反射来动态加载第三方类以及获取类的源码。 首先,我们要理解动态加载第三方类的概念。在传统的...

    php 反射简单例子

    在PHP编程语言中,反射(Reflection)是一种强大的工具,它允许程序在运行时检查自身的行为。这个特性在很多场合非常有用,比如动态加载类、分析类的方法和属性、执行注解处理等。本文将深入探讨PHP反射的工作原理,...

    PHP 反射(Reflection)使用实例

    PHP反射(Reflection)是PHP提供的一种强大的元编程技术,它允许程序在运行时检查自身的行为和结构。通过Reflection API,我们可以获取关于类、接口、函数、方法、参数、属性等对象的详细信息,并且能够在运行时动态地...

    reflection_api_doc:这是一个PHP自动生成api文档的库

    这是一个基于 thinkphp5.0 PHP自动生成api文档的库 两种使用方式,1.composer安装使用。2.tp5独立安装使用。 composer 方式安装 安装: 安装有两种方法: 直接执行: composer require "opqnext/reflection-api-doc:v...

Global site tag (gtag.js) - Google Analytics