小九博客

  • 首页
  • 编程开发
  • 信息安全
  • 工具资源
  • 随笔
  • 在线工具
    • 在线图片水印
    • Json解析
    • JavaRuntimeExec
    • 加解密/编码工具集
  • 关于
小九博客
Hack The World!
  1. 首页
  2. 编程开发
  3. 正文

PHP反射机制

2018年01月15日

简介

在PHP5以后,加入了一个反射(Reflection)类,使用此类可以方便的获取某类的属性、常量、方法等。基本涵盖了想要获取类的所有信息。也可以扩展自身类,为使用类的人提供更多信息,而不必阅读源代码。

可以使用的场景非常多,比如自动生成文档,自动化插件等。

常用方法

  • 获取常量列表
  • 获取方法列表
  • 获取属性列表

由于使用比较简单,其它请参考官方文档。
完整的方法列表请点击查看

我们定义一个学生类,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
class Student{
const SCHOOL = 'BeiJing Primary School';
const OBLIGATORY_COURSE = 'CHINESE';
public $name;
public $sex;
public $id;

public function getName(){
   return $this->name;
 }
public function getSex(){
return $this->sex;
}
public function getId(){
return $this->id;
}
public function setName($name){
$this->name = $name;
}
public function setSex($sex){
$this->sex = $sex;
}
public function setId($id){
$this->id = $id;
}
}
?>

首先new一个反射类,里面参数写上你想获取类的类名。

1
2
$objectClass = new \ReflectionClass('Student');
//$objectClass->[方法]
  • 获取常量列表 => public array getConstants ( void )

    1
    2
    $constArray = $objectClass->getConstants();
    var_dump($constArray);

     将打印如下结果:

    1
    2
    3
    4
    5
    6
    array(2) {
    ["SCHOOL"]=>
    string(22) "BeiJing Primary School"
    ["OBLIGATORY_COURSE"]=>
    string(7) "CHINESE"
    }
  • 获取方法列表 => public array ReflectionClass::getMethods ([ int $filter ] )
    参数filter,对方法列表进行过滤,默认不过滤。
     可使用提供的常量进行过滤
     - ReflectionMethod::IS_STATIC

    • ReflectionMethod::IS_PUBLIC
    • ReflectionMethod::IS_PROTECTED
    • ReflectionMethod::IS_PRIVATE
    • ReflectionMethod::IS_ABSTRACT
    • ReflectionMethod::IS_FINAL
      1
      2
      $methodArray = $objectClass->getMethods();
      var_dump($methodArray);
      将打印如下结果:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
          array(6) {
      [0]=>
      object(ReflectionMethod)#2 (2) {
      ["name"]=>
      string(7) "getName"
      ["class"]=>
      string(7) "Student"
      }
      [1]=>
      object(ReflectionMethod)#3 (2) {
      ["name"]=>
      string(6) "getSex"
      ["class"]=>
      string(7) "Student"
      }
      [2]=>
      object(ReflectionMethod)#4 (2) {
      ["name"]=>
      string(5) "getId"
      ["class"]=>
      string(7) "Student"
      }
      [3]=>
      object(ReflectionMethod)#5 (2) {
      ["name"]=>
      string(7) "setName"
      ["class"]=>
      string(7) "Student"
      }
      [4]=>
      object(ReflectionMethod)#6 (2) {
      ["name"]=>
      string(6) "setSex"
      ["class"]=>
      string(7) "Student"
      }
      [5]=>
      object(ReflectionMethod)#7 (2) {
      ["name"]=>
      string(5) "setId"
      ["class"]=>
      string(7) "Student"
      }
      }
  • 获取属性列表 => public array ReflectionClass::getProperties ([ int $filter ] )

     参数filter用于过滤结果,可用常量如下:

    • ReflectionProperty::IS_STATIC
      指示了 static 的属性。

    • ReflectionProperty::IS_PUBLIC
      指示了 public 的属性。

    • ReflectionProperty::IS_PROTECTED
      指示了 protected 的属性。

    • ReflectionProperty::IS_PRIVATE
      指示了 private 的属性。

1
2
$propertyArray = $objectClass->getProperties();
var_dump($propertyArray);

将打印如下结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
array(3) {
[0]=>
object(ReflectionProperty)#2 (2) {
["name"]=>
string(4) "name"
["class"]=>
string(7) "Student"
}
[1]=>
object(ReflectionProperty)#3 (2) {
["name"]=>
string(3) "sex"
["class"]=>
string(7) "Student"
}
[2]=>
object(ReflectionProperty)#4 (2) {
["name"]=>
string(2) "id"
["class"]=>
string(7) "Student"
}
}
标签 PHP
最后更新:2018年01月15日

文章评论

小九

Just For Fun

文章大纲
  1. 简介
  2. 常用方法
分类目录
  • 编程开发
  • Yii2
  • 随笔
  • 工具资源
  • Django
  • 信息安全
标签聚合
PHPUnit v2ray cloudflare PHP codeception yii2-事件
随机 最新 热点
随机 最新 热点
Yii2-Editable插件【GridView编辑】 Codeception安装使用(一) 【[转]PHP资源大全】Awesome PHP 项目 Yii2 Audit插件的使用【日志记录插件】 自动化编排学习(一)部署篇 MySQL数据同步到ElasticSearch(Logstash方案)爬坑纪实
OpenSearch集群部署 DNSRebind攻击 MySQL数据同步到ElasticSearch(Logstash方案)爬坑纪实 自动化编排学习(一)部署篇 常见容器漏洞总结 免费CDN加速手把手教程

COPYRIGHT © 2021 小九博客 ALL RIGHTS RESERVED.