php反射类的使用及Laravel对反射的使用介绍
PHP的反射类与实例化对象作用相反,实例化是调用封装类中的方法、成员,而反射类则是拆封类中的所有方法、成员变量,并包括私有方法等。就如“解刨”一样,我们可以调用任何关键字修饰的方法、成员。当然在正常业务中是建议不使用,比较反射类已经摒弃了封装的概念。
本章讲解反射类的使用及Laravel对反射的使用。 反射
反射类是PHP内部类,无需加载即可使用,你可以通过实例化 ReflectionClass 类去使用它。 方法
这里列举下PHP反射类常用的方法
方法名 注释
ReflectionClass::getConstant 获取定义过的一个常量
ReflectionClass::getConstants 获取一组常量
ReflectionClass::getConstructor 获取类的构造函数
ReflectionClass::getDefaultProperties 获取默认属性
ReflectionClass::getDocComment 获取文档注释
ReflectionClass::getEndLine 获取最后一行的行数
ReflectionClass::getFileName 获取定义类的文件名
ReflectionClass::getInterfaceNames 获取接口(interface)名称
ReflectionClass::getMethods 获取方法的数组
ReflectionClass::getModifiers 获取类的修饰符
ReflectionClass::getName 获取类名
ReflectionClass::getNamespaceName 获取命名空间的名称
ReflectionClass::getParentClass 获取父类
等等等等.... 所有关于类的方法、属性及其继承的父类、实现的接口都可以查询到,详见官网
栗子
<?php
namespace A\B;
class Foo { }
$function = new \ReflectionClass('stdClass');
var_dump($function->inNamespace());
var_dump($function->getName());
var_dump($function->getNamespaceName());
var_dump($function->getShortName());
$function = new \ReflectionClass('A\\B\\Foo');
var_dump($function->inNamespace());
var_dump($function->getName());
var_dump($function->getNamespaceName());
var_dump($function->getShortName());
?>
输出结果 bool(false)
string(8) "stdClass"
string(0) ""
string(8) "stdClass"
bool(true)
string(7) "A\B\Foo"
string(3) "A\B"
string(3) "Foo"
Laravel在实现服务容器加载时使用了反射类。现在我们开启“解刨”模式
入口文件
index.php
$app = require_once __DIR__.'/../bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
是引用语句发生的下一行调用了make方法。各位很清楚,make方法用于解析类,所有make方法的实现一定是在引用的文件内。 bootstrap\app.php
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
laravel开始加载它的核心类,所有的实现从 Illuminate\Foundation\Application 开始。
Illuminate\Foundation\Application
public function make($abstract, array $parameters = [])
{
$abstract = $this->getAlias($abstract); if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) { $this->loadDeferredProvider($abstract); }
return parent::make($abstract, $parameters);
}
在核心类中你可能准确的查找到make方法的存在,它加载了服务提供者随后调用了父类的方法make,要知道作为独立的模块 “服务容器”是绝对不能写在核心类的。懂点设计模式的都很清楚。
Illuminate\Container\Container
以 $api = $this->app->make('HelpSpot\API',['id'=>1]); 为例来讲解
// 真正的make方法,它直接调用了resolve继续去实现make的功能
// $abstract = 'HelpSpot\API'
public function make($abstract, array $parameters = [])
{
// $abstract = 'HelpSpot\API'
return $this->resolve($abstract, $parameters);
} ... protected function resolve($abstract, $parameters = [])
{
...
// 判断是否可以合理反射
// $abstract = 'HelpSpot\API'
if ($this->isBuildable($concrete, $abstract)) {
// 实例化具体实例 (实际并不是实例化,而是通过反射“解刨”了)
$object = $this->build($concrete);
} else {
$object = $this->make($concrete);
}
...
} public function build($concrete)
{
// $concrete = 'HelpSpot\API'
if ($concrete instanceof Closure) {
return $concrete($this, $this->getLastParameterOverride());
}
// 实例化反射类
$reflector = new ReflectionClass($concrete);
// 检查类是否可实例化
if (! $reflector->isInstantiable()) {
return $this->notInstantiable($concrete);
} $this->buildStack[] = $concrete;
// 获取类的构造函数
$constructor = $reflector->getConstructor();
if (is_null($constructor)) {
array_pop($this->buildStack);
return new $concrete;
}
$dependencies = $constructor->getParameters();
$instances = $this->resolveDependencies(
$dependencies
); array_pop($this->buildStack);
// 从给出的参数创建一个新的类实例。
return $reflector->newInstanceArgs($instances); }
可见一个服务容器就加载成功了。
php反射类的使用及Laravel对反射的使用介绍的更多相关文章
- C# 通过反射类动态调用DLL方法
网上看了很多关于反射的思路和方法,发现这个还算不错 //使用反射方: using System; using System.Collections.Generic; using System.Linq ...
- PHP的反射类ReflectionClass、ReflectionMethod使用实例
PHP5 具有完整的反射API,添加对类.接口.函数.方法和扩展进行反向工程的能力. 反射是什么? 它是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类.方法.属性.参数等的详细信息,包括 ...
- 反射类属性生成DataTable
public class People //类名 { private static string name; //字段 private string sex;//字段 public string Se ...
- C#语法糖之 ReflectionSugar 通用反射类
用法很简单: ReflectionSugar rs = new ReflectionSugar(100);//缓存100秒 ,可以不填默认不缓存 rs.有嘛点嘛 性能测试: 性能测试类源码: ht ...
- Java反射机制(获取Class对象的三种方式+获取Class中的构造函数进行对象的初始化+获取反射类的字段+获取反射类的一般方法)
反射技术其实就是动态加载一个指定的类,并获取该类中的所有内容.而且将字节码文件封装成对象,并将字节码文件中的内容都封装成对象,这样便于操作这些成员,简单来说:反射技术可以对一个类进行解剖,反射大大增强 ...
- 01_反射_04_反射类的main方法
[User.java] package com.Higgin.reflect; public class User { public User(){ System.out.println(" ...
- (转载)php反射类 ReflectionClass
(转载)http://hi.baidu.com/daihui98/item/a67dfb8213055dd75f0ec165 php反射类 ReflectionClass 什么是php反射类,可以 ...
- php反射类 ReflectionClass
什么是php反射类,顾名思义,能够理解为一个类的映射.举个样例: class fuc { //定义一个类static function ec() {echo '我是一个类';}}$cla ...
- PHP 反射类学习记录
原文:http://www.upwqy.com/details/58.html 1 开发环境 windows TP5 参考文档 http://php.net/manual/zh/class.refle ...
随机推荐
- C# WinForm文章收集
DataGridView 使用方法集锦 https://blog.csdn.net/zhaoyu_m69/article/details/70307934 关于DataGridView的一些操作(很全 ...
- Js中this机制全解
JavaScript中有很多令人困惑的地方,或者叫做机制. 但是,就是这些东西让JavaScript显得那么美好而与众不同. 比方说函数也是对 象.闭包.原型链继承等等,而这其中就包括颇让人费解的th ...
- javascript 字符串的连接和截取
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU - 3521 An easy Problem(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...
- web.py 模板错误记录
错误信息 Traceback (most recent call last): File , in process return self.handle() File , in handle retu ...
- buildroot构建项目(八)--- u-boot 2017.11 适配开发板修改 5 ---- 系统启动初始化之五
执行完 board_init_f 后,跳回到 crt0.S中继续执行汇编语言 ldr r0, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp, ...
- luogu 2294 狡猾的商人 带权并查集
此题做法多啊 带权并查集,区间dp,前缀和,差分约束 1.自己写的前缀和, 11 #include<bits/stdc++.h> #define rep(i,x,y) for(regist ...
- react框架的状态管理
安装: cnpm install --save redux cnpm install --save react-redux 安装好后导入模块内容: impor {createStore} from ...
- 3d图像识别基础论文:pointNet阅读笔记
PointNet 论文阅读: 主要思路:输入独立的点云数据,进行变换不变性处理(T-net)后,通过pointNet网络训练后,最后通过最大池化和softMax分类器,输出评分结果. 摘要: 相较于之 ...
- Linux内核驱动之延时 【转】
转自:http://blog.chinaunix.net/uid-24219701-id-3288103.html jiffies 计数器 定时器中断由系统定时硬件以规律地间隔产生; 这个间隔在启动 ...