当前位置: 移动技术网 > IT编程>开发语言>PHP > Laravel框架源码解析之反射的使用详解

Laravel框架源码解析之反射的使用详解

2020年06月14日  | 移动技术网IT编程  | 我要评论

明星身份证,水中月亮简谱,大西北织梦模板

本文实例讲述了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

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);
}

可见一个服务容器就加载成功了。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网