zoukankan      html  css  js  c++  java
  • laravel中如何利用反射实现依赖注入

    依赖注入

    在一个类中经常会依赖于其他的对象,先看一下经典的写法

    class Foo {
        public $bar;
        
        public function __construct() {
            $this->bar = new Bar();
        }
    }
    
    $foo = new Foo();
    

    当类的依赖发生改变时,比如 Bar 这个类需要实例化参数时,而依赖于它的类有很多,总不能一个一个地去修改吧。

    再看一下使用 依赖注入 怎么做

    class Foo {
        public $bar;
        
        public function __construct($bar) {
            $this->bar = $bar;
        }
    }
    
    $bar = new Bar();
    //$bar = new Bar($args);
    $foo = $foo = new Foo($bar);
    

    Bar 类在外部实例化好后,作为一个参数传入进 Foo 类,从而实现了 控制反转 ,假如现在 Bar 类需要参数了,外部修改就好了,不必一个个地去修改依赖于它的类。

    laravel中的依赖注入

    laravel 中,经常写出下面这种代码

    class SomeController {
        public function index(Request $request) {
            dd($request->all());
        }
    }
    

    只要在方法参数中申明 Request $request,就可以直接使用 $request 对象了,非常地方便。

    其实laravel在背后利用PHP的反射机制为我们做了 $request = new Request 这一步。反射是一种类的反省能力,可以导出类的详细信息包括属性、方法、甚至注释等等。关于反射可以查看PHP文档http://php.net/manual/zh/intro.reflection.php

    实现,看代码

    $method = new ReflectionMethod('SomeController', 'index');
    $args = [];
    foreach($method->getParameters() as $parameter) {
        if ($class = $parameter->getClass()) {
            $args[] = new $class->name; //$request = new Request
        }
    }
    $method->invokeArgs(new SomeController, $args);
    

    通过 ReflectionMethod 获取类方法的参数,如果参数是其他的类,就实例化后作为参数使用 ReflectionMethod::invokeArgs 传入到类方法中,原理就是这么简单。

    通常使用 new ReflectionClass('className') 来反射类,ReflectionMethod 来反射类方法。

  • 相关阅读:
    Feign (配合Hystrix) +文件传输
    springCloud Euraka (HA && docker)
    MySQL表结构映射为Python中的对象 python (SQLAlchemy)
    sqoop 脚本
    ubuntu16 ntp时钟同步服务设置
    Bootstrap3基础教程 03 导航栏
    Bootstrap3基础教程 02 网格布局
    Bootstrap3基础教程 01 概述
    C#面向对象21 接口
    C#面向对象20 序列化和反序列化
  • 原文地址:https://www.cnblogs.com/luke44/p/laravel-dependencies-inject.html
Copyright © 2011-2022 走看看