原文: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"

PHP 反射类学习记录的更多相关文章

  1. Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 在 ...

  2. Lua和C++交互 学习记录之八:C++类注册为Lua模块

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...

  3. MyBatis 学习记录3 MapperMethod类

    主题 之前学习了一下MapperProxy的生产过程,自定义Mapper类的对象是通过动态代理生产的,调用自定义方法的时候实际上是调用了MapperMethod的execute方法:mapperMet ...

  4. Quartz 学习记录1

    原因 公司有一些批量定时任务可能需要在夜间执行,用的是quartz和spring batch两个框架.quartz是个定时任务框架,spring batch是个批处理框架. 虽然我自己的小玩意儿平时不 ...

  5. Java设计模式学习记录-单例模式

    前言 已经介绍和学习了两个创建型模式了,今天来学习一下另一个非常常见的创建型模式,单例模式. 单例模式也被称为单件模式(或单体模式),主要作用是控制某个类型的实例数量是一个,而且只有一个. 单例模式 ...

  6. Elasticsearch、XXLJob以及最近的学习记录

    Elasticsearch.XXLJob以及最近的学习记录 前言 在这九月的最后一周,来总结一下最近的学习记录,主要是对于Elasticsearch.XXLjob的初步学习,想着还是多记录点,以便后面 ...

  7. Java 8 学习记录

    Java 8 学习记录 官方文档 https://docs.oracle.com/javase/8/ https://docs.oracle.com/javase/8/docs/index.html ...

  8. Java 静态内部类与非静态内部类 学习记录.

    目的 为什么会有这篇文章呢,是因为我在学习各种框架的时候发现很多框架都用到了这些内部类的小技巧,虽然我平时写代码的时候基本不用,但是看别人代码的话至少要了解基本知识吧,另外到底内部类应该应用在哪些场合 ...

  9. Apache Shiro 学习记录4

    今天看了教程的第三章...是关于授权的......和以前一样.....自己也研究了下....我觉得看那篇教程怎么说呢.....总体上是为数不多的精品教程了吧....但是有些地方确实是讲的太少了.... ...

随机推荐

  1. 听闰土大话前端之ES6是怎么来的

    前言 相信做前端的朋友没有不知道ECMAScript6的,都知晓ES6新增了不少新的特性,但是你知道ES6是怎么来的吗?今天就让闰土来带大家大话ES6的前世今生.当然了,这篇文章会以扫盲为主,科普为辅 ...

  2. Java JMS 程序基础 与 ActiveMQ 安装(一)

    一 ActiveMQ安装 从Apache官网上下载 ActivieMQ的安装包 apache-activemq-5.9.1-bin.tar.gz, 并拷贝到linux的安装目录解压 # tar -zx ...

  3. 端到端测试工具--testcafe

    写在前面 随着业务的增加,复杂性的增加,我们更需要保证页面不能出错,之前需要每次上线之前需要每次人工测试,如果有好多改动,为保证业务不出错,需要耗费更多的时间来测试,所以我们需要写一些测试来保证业务的 ...

  4. CentOS Crontab(定时任务)

    安装crontab: yum install crontabs 说明: service crond start //启动服务 service crond stop //关闭服务 service cro ...

  5. python针对端口11211进行全网收集

    前言: 最近Memcached分布式系统DRDoS拒绝服务攻击 一夜之内流量暴增.各种网站给打挂.原先打算写 一个poc可惜失败了. 0x01 requests模块 0x02 去钟馗之眼注册一个账号, ...

  6. 使用commons-csv简单读写CSV文件

    文章首发于我的github博客 需求 客户的开发测试环境将做迁移.因此需要对zookeeper上的重要的数据以CSV文件格式做备份. 本文通过Apache的commons-csv操作CSV文件.官网地 ...

  7. Java集合框架(三)—— List、ArrayList、Vector、Stack

    List接口 List集合代表一个有序集合,集合中每一个元素都有其对应的顺序索引.List集合容许使用重复元素,可以通过索引来访问指定位置的集合对象. ArrayList和Vector实现类 Arra ...

  8. 【开源】C#.NET股票历史数据采集,【附18年历史数据和源代码】

    如果用知乎,可以关注专栏:.NET开源项目和PowerBI社区 重点重点:我没有买股票,没有买股票,股市是个坑,小心割韭菜哦. 本文的初衷是数据分析(分析结果就不说了,就是想看看筛选点数据),只不过搞 ...

  9. AWS EC2 通过Linux终端:使用ssh连接到Linux实例

    AWS的ubuntu主机登录用户是ubuntu 只能通过秘钥的方式登录 如果在linux终端通过ssh远程登录步骤如下: 假如申请EC2主机的时候下载的key名称叫my-key.pem,并保存在本地l ...

  10. 内置函数 -- filter 和 map

    参考地址:http://www.cnblogs.com/sesshoumaru/p/6000788.html 英文文档: filter(function, iterable) Construct an ...