当前位置:Gxlcms > PHP教程 > php反射类的使用及Laravel对反射的使用介绍

php反射类的使用及Laravel对反射的使用介绍

时间:2021-07-01 10:21:17 帮助过:15人阅读

本篇文章给大家带来的内容是关于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获取父类

等等等等.... 所有关于类的方法、属性及其继承的父类、实现的接口都可以查询到。
详细文档请参考官网

栗子

  1. <?php
  2. namespace A\B;
  3. class Foo { }
  4. $function = new \ReflectionClass('stdClass');
  5. var_dump($function->inNamespace());
  6. var_dump($function->getName());
  7. var_dump($function->getNamespaceName());
  8. var_dump($function->getShortName());
  9. $function = new \ReflectionClass('A\\B\\Foo');
  10. var_dump($function->inNamespace());
  11. var_dump($function->getName());
  12. var_dump($function->getNamespaceName());
  13. var_dump($function->getShortName());
  14. ?>

输出结果

  1. bool(false)
  2. string(8) "stdClass"
  3. string(0) ""
  4. string(8) "stdClass"
  5. bool(true)
  6. string(7) "A\B\Foo"
  7. string(3) "A\B"
  8. string(3) "Foo"

Laravel

Laravel在实现服务容器加载时使用了反射类。现在我们开启“解刨”模式

入口文件

index.php

  1. $app = require_once __DIR__.'/../bootstrap/app.php';
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Run The Application
  5. |--------------------------------------------------------------------------
  6. |
  7. | Once we have the application, we can handle the incoming request
  8. | through the kernel, and send the associated response back to
  9. | the client's browser allowing them to enjoy the creative
  10. | and wonderful application we have prepared for them.
  11. |
  12. */
  13. $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
  14. $response = $kernel->handle(
  15. $request = Illuminate\Http\Request::capture()
  16. );
  17. $response->send();
  18. $kernel->terminate($request, $response);

是引用语句发生的下一行调用了make方法。各位很清楚,make方法用于解析类,所有make方法的实现一定是在引用的文件内。

bootstrap\app.php

  1. $app = new Illuminate\Foundation\Application(
  2. realpath(__DIR__.'/../')
  3. );

laravel开始加载它的核心类,所有的实现从 Illuminate\Foundation\Application 开始。

Illuminate\Foundation\Application

  1. public function make($abstract, array $parameters = [])
  2. {
  3. $abstract = $this->getAlias($abstract);
  4. if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) {
  5. $this->loadDeferredProvider($abstract);
  6. }
  7. return parent::make($abstract, $parameters);
  8. }

在核心类中你可能准确的查找到make方法的存在,它加载了服务提供者随后调用了父类的方法make,要知道作为独立的模块 “服务容器”是绝对不能写在核心类的。懂点设计模式的都很清楚。

Illuminate\Container\Container

$api = $this->app->make('HelpSpot\API',['id'=>1]); 为例来讲解

  1. // 真正的make方法,它直接调用了resolve继续去实现make的功能
  2. // $abstract = 'HelpSpot\API'
  3. public function make($abstract, array $parameters = [])
  4. {
  5. // $abstract = 'HelpSpot\API'
  6. return $this->resolve($abstract, $parameters);
  7. }
  8. ...
  9. protected function resolve($abstract, $parameters = [])
  10. {
  11. ...
  12. // 判断是否可以合理反射
  13. // $abstract = 'HelpSpot\API'
  14. if ($this->isBuildable($concrete, $abstract)) {
  15. // 实例化具体实例 (实际并不是实例化,而是通过反射“解刨”了)
  16. $object = $this->build($concrete);
  17. } else {
  18. $object = $this->make($concrete);
  19. }
  20. ...
  21. }
  22. public function build($concrete)
  23. {
  24. // $concrete = 'HelpSpot\API'
  25. if ($concrete instanceof Closure) {
  26. return $concrete($this, $this->getLastParameterOverride());
  27. }
  28. // 实例化反射类
  29. $reflector = new ReflectionClass($concrete);
  30. // 检查类是否可实例化
  31. if (! $reflector->isInstantiable()) {
  32. return $this->notInstantiable($concrete);
  33. }
  34. $this->buildStack[] = $concrete;
  35. // 获取类的构造函数
  36. $constructor = $reflector->getConstructor();
  37. if (is_null($constructor)) {
  38. array_pop($this->buildStack);
  39. return new $concrete;
  40. }
  41. $dependencies = $constructor->getParameters();
  42. $instances = $this->resolveDependencies(
  43. $dependencies
  44. );
  45. array_pop($this->buildStack);
  46. // 从给出的参数创建一个新的类实例。
  47. return $reflector->newInstanceArgs($instances);
  48. }

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

以上就是php反射类的使用及Laravel对反射的使用介绍的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行