首页 技术 正文
技术 2022年11月21日
0 收藏 325 点赞 3,218 浏览 5714 个字

原文:http://www.upwqy.com/details/58.html

1 开发环境

windows

TP5

参考文档

http://php.net/manual/zh/class.reflectionclass.php

2 准备工作

新建一个测试反射类 TestReflection.php

<?php
/**
* User: [一秋]
* Date: 2017-11-23
* Time: 16:29
* Desc: 成功来源于点滴
*/namespace app\api\controller\v3;class TestReflection
{
/**
* @title 首页
*/
public function index(){
echo 'index';
}
public function add(){
echo 'add';
}}

在新建 一个类  Test.php

3 测试

3.1 ReflectionClass::__construct — 初始化 ReflectionClass 类

public ReflectionClass::__construct ( mixed $argument )

public function index(){
$method = new \ReflectionClass('app\api\controller\v3\TestReflection');
dump($method);
}

返回结果是

object(ReflectionClass)#12 (1) {
  ["name"] => string(26) "app\api\controller\v3\TestReflection"
}

3.2 ReflectionClass::export — 导出一个类

public static string ReflectionClass::export ( mixed $argument [, bool $return = false ] )

public function index(){
$method = new \ReflectionClass('ReflectionClass');
\Reflection::export($method);}

返回的结果是:

Class [ class app\api\controller\v3\TestReflection ] { @@ D:\phpStudy\WWW\mmks\application\api\controller\v3\TestReflection.php 12-21 – Constants [0] { } – Static properties [0] { } – Static methods [0] { } – Properties [0] { } – Methods [2] { Method [ public method index ] { @@ D:\phpStudy\WWW\mmks\application\api\controller\v3\TestReflection.php 14 – 16 } Method [ public method add ] { @@ D:\phpStudy\WWW\mmks\application\api\controller\v3\TestReflection.php 17 – 19 } } }

3.3 ReflectionClass::getConstant — 获取定义过的一个常量

public mixed ReflectionClass::getConstant ( string $name )

获取定义过的一个常量。

在TestReflection 新增一个常量

<?php
/**
* User: [一秋]
* Date: 2017-11-23
* Time: 16:29
* Desc: 成功来源于点滴
*/
namespace app\api\controller\v3;
class TestReflection
{
const NAME = 'wang';
/**
* @title 首页
*/
public function index(){
echo 'index';
}
public function add(){
echo 'add';
}}

Test.php

<?php
/**
* User: [一秋]
* Date: 2017-11-23
* Time: 15:59
* Desc: 成功来源于点滴
*/namespace app\api\controller\v3;class Test
{
public function index(){
$method = new \ReflectionClass('app\api\controller\v3\TestReflection');
//导出一个类
// \Reflection::export($method);
$name = $method->getConstant('NAME');
dump($name);
}
}

返回的结果是

string(4) "wang"

这里如果常量不存在 则返回false

3.4 ReflectionClass::getConstants — 获取一组常量

public array ReflectionClass::getConstants ( void )

这里的 getConstants 不要拼错了。比上一个方法多了一个 s

获取某个类的全部已定义的常量,不管可见性如何定义。

本函数还未编写文档,仅有参数列表。

<?php
/**
* User: [一秋]
* Date: 2017-11-23
* Time: 15:59
* Desc: 成功来源于点滴
*/namespace app\api\controller\v3;class Test
{
public function index(){
$method = new \ReflectionClass('app\api\controller\v3\TestReflection');
//导出一个类
// \Reflection::export($method);
$name = $method->getConstants();
dump($name);
}
}

返回的结果是

array(1) {
  ["NAME"] => string(4) "wang"
}

3.5 ReflectionClass::getConstructor — 获取类的构造函数

public ReflectionMethod ReflectionClass::getConstructor ( void )

获取已反射的类的构造函数。

为了方便测试  这里映射自身

<?php
/**
* User: [一秋]
* Date: 2017-11-23
* Time: 15:59
* Desc: 成功来源于点滴
*/namespace app\api\controller\v3;class Test
{
const NAME = 'wang';
public function __construct()
{
} public function index(){
$method = new \ReflectionClass(__CLASS__);
//导出一个类
// \Reflection::export($method);
$name = $method->getConstructor();
dump($name);
}
}

返回的结果是

object(ReflectionMethod)#13 (2) {
  ["name"] => string(11) "__construct"
  ["class"] => string(26) "app\api\controller\v3\Test"
}

3.6 ReflectionClass::getDefaultProperties — 获取默认属性

public array ReflectionClass::getDefaultProperties ( void )

获取类的默认属性(包括了继承的属性)。

参数

此函数没有参数。

返回值

默认属性的数组,其键是属性的名称,其值是属性的默认值或者在属性没有默认值时是 NULL。 这个函数不区分静态和非静态属性,也不考虑可见性修饰符。

<?php
/**
* User: [一秋]
* Date: 2017-11-23
* Time: 15:59
* Desc: 成功来源于点滴
*/namespace app\api\controller\v3;/**
* 反射测试类
* Class Test
* @package app\api\controller\v3
*/
class Test extends par
{
public $user = 'user';
private $name = 'name';
public static $tel = '12345678912';
public $pass; const NAME = 'wang'; /**
* 构造函数
* Test constructor.
*/
public function __construct()
{
} /**
* 测试入口
* @param int $id 没有id
*/
public function index(){
$method = new \ReflectionClass(__CLASS__);
$name = $method->getDefaultProperties();
dump($name);
}
}
class par{
protected $par = 'par';
}

返回的结果是

array(5) {
  ["tel"] => string(11) "12345678912"
  ["user"] => string(4) "user"
  ["name"] => string(4) "name"
  ["pass"] => NULL
  ["par"] => string(3) "par"
}

3.7 ReflectionClass::getDocComment — 获取文档注释

public string ReflectionClass::getDocComment ( void )

从一个类中获取文档注释。

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

如果存在则返回文档注释,否则返回 FALSE

<?php
/**
* User: [一秋]
* Date: 2017-11-23
* Time: 15:59
* Desc: 成功来源于点滴
*/namespace app\api\controller\v3;/**
* 反射测试类
* Class Test
* @package app\api\controller\v3
*/
class Test extends par
{
public $user = 'user';
private $name = 'name';
public static $tel = '12345678912';
public $pass; const NAME = 'wang'; /**
* 构造函数
* Test constructor.
*/
public function __construct()
{
} /**
* 测试入口
* @param int $id 没有id
*/
public function index(){
$method = new \ReflectionClass(__CLASS__);
$name = $method->getDocComment();
dump($name);
}
}/**
* Class par
* @package app\api\controller\v3
*/
class par{
protected $par = 'par';
}

返回结果是

string(78) "/**
 * 反射测试类
 * Class Test
 * @package app\api\controller\v3
 */"

3.8 ReflectionClass::getEndLine — 获取最后一行的行数

public int ReflectionClass::getEndLine ( void )

从用户定义的类获取其最后一行的行数。

参数

此函数没有参数。

返回值

返回用户定义的类最后一行的行数,如果未知则返回 FALSE


    /**
* 测试入口
* @param int $id 没有id
*/
public function index(){
$method = new \ReflectionClass(__CLASS__);
$name = $method->getEndLine();
dump($name);
}

返回结果是

int(39)

3.9 ReflectionClass::getExtension — 根据已定义的类获取所在扩展的 ReflectionExtension 对象

public ReflectionExtension ReflectionClass::getExtension ( void )

获取已定义类的扩展的 ReflectionExtension 对象。

参数

此函数没有参数。

返回值

类所处的扩展的 ReflectionExtension 对象的表示,如果是用户定义的类则返回 NULL

    /**
* 测试入口
* @param int $id 没有id
*/
public function index(){
$method = new \ReflectionClass('Reflection');
$name = $method->getExtension();
dump($name);
}

返回的结果是

object(ReflectionExtension)#13 (1) {
  ["name"] => string(10) "Reflection"
}

3.10  ReflectionClass::getExtensionName — 获取定义的类所在的扩展的名称

说明

public string ReflectionClass::getExtensionName ( void )

获取定义的类所在的扩展的名称。

参数

此函数没有参数。

返回值

获取定义的类所在的扩展的名称,如果是用户定义的类,则返回 FALSE

public function index(){
$method = new \ReflectionClass('ReflectionClass');
$name = $method->getExtensionName();
dump($name);
}

返回的结果是

string(10) "Reflection"
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860