Laravel 依赖注入原理

众所周知 Laravel 的文档对于依赖注入只写了如何使用,相信大多数人对于他的实现原理并不太清楚。虽然使用过程中并不需要关心她的原理,但是了解原理让你使用起来更自信。这个帖子就通过一个小 demo 来简述实现原理,demo 如下,该 demo 可直接运行:

<?php

namespace Database;
use ReflectionMethod;

class Database
{

    protected $adapter;

    public function __construct ()
    {}

    public function test (MysqlAdapter $adapter)
    {
        $adapter->test();
    }
}

class MysqlAdapter
{

    public function test ()
    {
        echo "i am MysqlAdapter test";
    }
}

class app
{

    public static function run ($instance, $method)
    {
        if (! method_exists($instance, $method))

            return null;

        $reflector = new ReflectionMethod($instance, $method);

        $parameters = [
                    1
            ];
        foreach ($reflector->getParameters() as $key => $parameter)
        {

            $class = $parameter->getClass();

            if ($class)
            {
                array_splice($parameters, $key, 0, [
                        new $class->name()
                ]);
            }
        }
        call_user_func_array([
                $instance,
                $method
        ], $parameters);
    }
}

app::run(new Database(), 'test');

原理主要运用了PHP反射api的 ReflectionMethod 类,在PHP运行状态中,扩展分析PHP程序。具体使用可查看手册。

本帖已被设为精华帖!
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 21
//array_splice($parameters, $key, 0, [
//                        new $class->name()
//                ]);
$parameters = [new $class->name()];

一个小小的问题:对array_splice这一句有困惑。

6年前 评论

简单注释一下备忘

class app
{

    public static function run($instance, $method)
    {
        if (!method_exists($instance, $method)) {
            return null;
        }

        //ReflectionMethod::__construct — Constructs a ReflectionMethod
        $reflector = new ReflectionMethod($instance, $method);
        //The ReflectionMethod class reports information about a method. See here:
        // http://php.net/manual/en/class.reflectionmethod.php

        //preset an array which can be empty or not.
        $parameters = [];

        //ReflectionFunctionAbstract::getParameters — Gets parameters
        //Get the parameters as an array of ReflectionParameter.
        //The ReflectionParameter class retrieves information about function's or method's parameters.
        //See here: http://php.net/manual/en/class.reflectionparameter.php
        foreach ($reflector->getParameters() as $key => $parameter) {
            //ReflectionParameter::getClass — Get the type hinted class.
            //http://php.net/manual/en/reflectionparameter.getclass.php
            $class = $parameter->getClass();

            if ($class) {
                //array_splice — Remove a portion of the array and replace it with something else.
                //If length is specified and is zero, no elements will be removed(That is insert!)
                array_splice($parameters, $key, 0, [
                    new $class->name()
                ]);
            }
        }
        //call_user_func_array — Call a callback with an array of parameters
        //So the code below is to invoke \Database\MysqlAdapter::test with a \Database\MysqlAdapter instance.
        call_user_func_array([
            $instance,
            $method
        ], $parameters);
    }
}
5年前 评论

依赖注入怎么能少得了更进一步的容器呢(/摊手)

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!