「轉」Laravel 依賴注入原理(詳細註釋)

hustnzj發表於2018-10-15

眾所周知 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;
        }

        //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\Database::test with a \Database\MysqlAdapter instance parameter.
        call_user_func_array([
            $instance,
            $method
        ], $parameters);
    }
}

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

相關文章