精华内容
下载资源
问答
  • laravel-container Laravel 容器是完全独立的,即使你的项目不是基于 ...想了解更多关于容器的知识?去读源码吧!容器在底层只有一个类 Illuminate\Container\Container 读完了你就会对容器如何工作有更深的理...

    laravel-container

    Laravel 容器是完全独立的,即使你的项目不是基于 Laravel 框架的,依然可以使用 Laravel 的服务容器,只要通过 Composer 安装 illuminate/container 就好了。想了解更多关于容器的知识?去读源码吧!容器在底层只有一个类 Illuminate\Container\Container 读完了你就会对容器如何工作有更深的理解。

    初始化composer.json

    vi composer.json

    {
        "name": "laravel/container",
        "description": "Laravel Container.",
        "keywords": ["container", "laravel"],
        "license": "MIT",
        "type": "project",
        "require": {
            "php": "^7.0.0"
        },
        "autoload": {
        },
        "minimum-stability": "dev",
        "prefer-stable": true
    }
    

    安装laravel-container

    composer require illuminate/container
    

    psr-4 autoload

    vi composer.json

    {
        "autoload": {
            "files": [
                "app/Support/helpers.php"
            ],
            "psr-4": {
                "App\\": "app/"
            }
        }
    }
    

    App\Application

    vi app/Application.php

    namespace App;
    
    use Illuminate\Container\Container;
    
    class Application extends Container
    {
    
        public function __construct()
        {
            static::setInstance($this);
        }
    
    }
    

    helpers.php

    vi app/Support/helpers.php

    function app($abstract = null, array $parameters = [])
    {
        if (is_null($abstract)) {
            return Illuminate\Container\Container::getInstance();
        }
    
        return Illuminate\Container\Container::getInstance()->make($abstract, $parameters);
    }
    

    index.php

    vi public/index.php

    require __DIR__ . '/../vendor/autoload.php';
    
    $app = new App\Application();
    
    $app->instance('name', 'Cuber');
    s($app->make('name'));
    s(app('name'));
    s(app());
    
    展开全文
  • (给PHP开发者加星标,提升...理解服务容器的概念,对于我们使用laravel太重要了,应该说是否理解服务容器的概念是区分是否入门laravel的重要条件。因为整个框架正是在服务容器这一基础上构建起来的。laravel服务容...

    (给PHP开发者加星标,提升PHP技能)

    转自:xiaoxingping

    segmentfault.com/a/1190000015463623

    如果说laravel框架的核心是什么,那么无疑是服务容器。理解服务容器的概念,对于我们使用laravel太重要了,应该说是否理解服务容器的概念是区分是否入门laravel的重要条件。因为整个框架正是在服务容器这一基础上构建起来的。

    ae5d9be0f9adf6c7ca10ca4f38456fa6.png

    laravel服务容器就像一个高度自动化的工厂,你需要的东西,定制好模型,使用特定接口来制造。

    因为使用了服务容器,laravel中大部分对象实例化的方式是这样的:

    $obj1 = $container->make('class1', 'class2');$obj2 = $container->make('class3''class4');

    但是在没有使用服务容器的情况下,以下这种方式同样可以做到:

    $obj1 = new class1(new class2());$obj2 = new class3(new class4());

    那么使用服务容器的优势到底是什么呢?

    下面我们通过一些具体例子来分析下它的优势:

    例一、发送邮件

    我们把发送邮件的功能封装成一个类,需要使用的时候,实例化并调用发送方法。

    以下是不使用laravel服务容器常见的方式:

    /** *发送邮件服务类 */class EmailService{    public function send(){        //todo 发送邮件方法    }}//如果任何地方要发邮件我们就复制下面这两行代码$emailService = new EmailService();$emailService->send();

    使用了了laravel服务容器以后:

    $this->app->bind('emailService'function ($app) {    return new EmailService();});//如果任何地方要发邮件我们就复制下面这两行代码$emailService = app('emailService');$emailService->send();

    这使得我们的代码更加简洁了,并且由于有了中间层,灵活性提高了(解耦),那么无论是测试(在测试时我们可以伪造类替换EmailService类)还是优化EmailService类,都变得更加方便。

    //只需要改这一个地方$this->app->bind('emailService', function ($app) {    return new SupperEmailService();});

    其他调用的部分我们完全不用动,如果我们没有这个绑定操作,那么不得不在每个使用邮件服务的地方做更改。

    //使用到EamilSerice类的每个地方都要更改$emailService = new SupperEmailService();$emailService->send();

    例二、实现单例模式

    还是上面的例子,出于性能的考虑,你需要SupperEamilService类实现单例模式,于是在不使用laravel服务容器的情况下,你将SupperEmailService类更改如下:

    class SupperEamilService{      //创建静态私有的变量保存该类对象     static private $instance;      //防止直接创建对象      private function __construct(){     }         //防止克隆对象     private function __clone(){     }     static public function getInstance(){                 //判断$instance是否是Uni的对象                 //没有则创建         if (!self::$instance instanceof self) {             self::$instance = new self();         }         return self::$instance;     }     //发送邮件方法     public function send(){     } }

    除此之外,由于现在SupperEamilService类构造函数为私有,无法通过new关键字来实例化对象,所以在每个实例化SupperEmailService类的地方都要改成这样:

    $emailService=SupperEmailService::getInstance();$emailService->send();

    laravel服务容器天生支持单例,下面是laravel的实现方式:

    //只需要把bind改成singleton $this->app->singleton('emailService', function ($app) {    return new SupperEmailService();});

    要实现单例甚至只需要改一行代码,把原来的bind方法改成singleton ,通过容器取出来的便是单例,真是太方便了。

    例三、旅行者去旅行

    这个例子假设一个旅行者去西藏旅行,可以做火车(train)或者走路(leg)去。

    不使用laravel服务容器:

    <?php interface TrafficTool{  public function go();}class Train implements TrafficTool{  public function go(){  echo "train....";  }}class Leg implements TrafficTool{  public function go(){  echo "leg..";  }}class Traveller{  /**  * @var Leg|null|Train  * 旅行工具  */  protected $_trafficTool;  public function __construct(TrafficTool $trafficTool){  $this->_trafficTool = $trafficTool;  }  public function visitTibet(){  $this->_trafficTool->go();  }}

    当旅行者要坐火车去旅行通常我们这样写:

    <?php  $train = new Train();$tra = new Traveller($train);$tra->visitTibet();

    事实上这种写法已经非常不错了,因为对于旅行工具的依赖已经通过接口的方式转移到外部了。

    但是使用new来实例化对象的时候还是会产生依赖.比如上面trafficTool),这说明我们要创建一个Traveller之前必须得有一个$trafficTool,即Traveller依赖于trafficTool.当使用new来实例化Traveller的时候,Traveller和trafficTool之间就产生了耦合.这样,这两个组件就没办法分开了。

    现在我们来看看使用laravel服务容器是怎么实现的:

    在服务容器中绑定类

    <?php namespace App\Providers;use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;class RepositoryServiceProvider extends ServiceProvider{  public function register(){     //在服务容器中绑定类     $this->app->bind( 'TrafficTool', 'Train');     $this->app->bind('Traveller', 'Traveller');  }}

    实例化对象

    <?php // 实例化对象$tra = app()->make('Traveller');$tra->visitTibet();

    当我们使用服务容器获取旅行类的对象时,容器会自动注入对象所需要的参数。而在此之前我只需要绑定特定的类就可以了,这样做才体现了真正的自动化,而且使得旅行类和旅行工具类完全解耦了。

    当我们需要更改旅行方式的时候,我们就只需要更改绑定就可以了。

    总结

    上面举了几个简单的例子,如果能完全理解和掌握laravel服务容器,实际开发中它会给你提供更多的便利。当然它也不是完美无缺的,下篇博客打算再来描述它的缺点,总之实际使用中扬长避短才是关键。

    推荐阅读  点击标题可跳转

    Laravel 成为最佳 PHP 框架的 14 个理由!

    Laravel 5.5 新特性预览

    PHP 框架 Laravel 作者的工作日常

    看完本文有收获?请分享给更多人

    关注「PHP开发者」加星标,提升PHP技能

    5124e1f9fffba7c783c7e5fa027d2316.png

    好文章,我在看❤️

    展开全文
  • laravel容器

    2019-12-31 17:52:26
    有时候使用双飞燕,有时候雷蛇 这两个统称为键盘,所以写一个接口类 interface Board { public function attr();//键盘属性 } 创建两个类: LeiShe.php <?php namespace App\Services\Test\board; ...

    本文为demo,更详情请查看这里

    电脑有键盘,比方:雷蛇,双飞燕。

    有时候使用双飞燕,有时候雷蛇

    这两个统称为键盘,所以写一个接口类

    interface Board
    {
        
        public function attr();//键盘的属性
    
    }
    
    

    创建两个类:

    LeiShe.php

    <?php
    
    
    namespace App\Services\Test\board;
    
    
    use App\Services\Test\Board;
    
    class LeiShe implements Board
    {
        public $attr='雷蛇键盘';
    
        public function attr()
        {
           return $this->attr;
        }
    }
    
    

    ShuangFeiYan.php

    <?php
    
    
    namespace App\Services\Test\board;
    
    
    use App\Services\Test\Board;
    
    class ShuangFeiYan implements Board
    {
    
        public $attr='双飞燕键盘';
    
        public function attr()
        {
            return $this->attr;
        }
    }
    
    

    创建 Provider

    php artisan make:provider TestService
    

    在 app/Providers
    生成:TestServiceProvider.php

    <?php
    
    namespace App\Providers;
    
    use App\Services\Test\Computer;
    use Illuminate\Support\ServiceProvider;
    
    class TestServiceProvider extends ServiceProvider
    {
        /**
         * Register services.
         *
         * @return void
         */
        public function register()
        {
    
            $this->app->singleton('LeiShe','App\Services\Test\board\LeiShe');//雷蛇
            $this->app->bind('ShuangFeiYan','App\Services\Test\board\ShuangFeiYan');//双飞燕
        }
    
        /**
         * Bootstrap services.
         *
         * @return void
         */
        public function boot()
        {
            //
        }
    }
    
    

    现在你可以在程序中使用

    echo app('LeiShe')->attr();
    

    singleton实现的是单例模式;

    bind: new 对象

    其他扩展:
    Computer.phlp

    <?php
    
    
    namespace App\Services\Test;
    
    
    class Computer
    {
        /**
         * @var
         */
        public $board;
    
        function __construct(Board $board)
        {
            $this->board = $board;
        }
    
        public function attr(){
    
            return $this->board->attr();
    
        }
    }
    
    

    提供者:

    class TestServiceProvider extends ServiceProvider
    {
        /**
         * Register services.
         *
         * @return void
         */
        public function register()
        {
    
            $this->app->singleton('LeiShe','App\Services\Test\board\LeiShe');//雷蛇
            $this->app->singleton('ShuangFeiYan','App\Services\Test\board\ShuangFeiYan');//双飞燕
    
            //触发事件
            $this->app->resolving('ShuangFeiYan',function ($object, $container){
                $object->memo = '初始化的值';
                $leishe = $container->make('LeiShe');
                $leishe->memo = '初始化雷蛇的默认值';
            });
    
            $this->app->bind('Computer',function ($container){
                return new Computer($container->make('ShuangFeiYan'));//使用双飞燕键盘
            });
    
    
    
        }
    

    和我做朋友?

    展开全文
  • Laravel illuminate/container 移除掉 illuminate/contracts 依赖,便于灵活在非 Laravel 框架中使用,顺便机翻了一下 illuminate/container 注释,便于理解 Laravel 的容器。 相关链接 ...

    中文注释版 Laravel 容器类(Container)

    将 Laravel 的 illuminate/container 移除掉 illuminate/contracts 依赖,便于灵活的在非 Laravel 框架中使用,顺便机翻了一下 illuminate/container 的注释,便于理解 Laravel 的容器。

    相关链接

    代码

    <?php
    
    /*
     * This file is part of the guanguans/di.
     *
     * (c) guanguans <ityaozm@gmail.com>
     *
     * This source file is subject to the MIT license that is bundled.
     */
    
    namespace Guanguans\Di;
    
    use ArrayAccess;
    use Closure;
    use LogicException;
    use Psr\Container\ContainerInterface;
    use ReflectionClass;
    use ReflectionParameter;
    
    /**
     * This file is modified from `illuminate/container`.
     *
     * @see https://github.com/illuminate/container
     */
    class Container implements ArrayAccess, ContainerInterface
    {
        /**
         * The current globally available container (if any).
         * 当前的全局可用容器(如果有)。
         *
         * @var static
         */
        protected static $instance;
    
        /**
         * An array of the types that have been resolved.
         * 已解析类型的数组。
         *
         * @var array
         */
        protected $resolved = [];
    
        /**
         * The container's bindings.
         * 容器的绑定。
         *
         * @var array
         */
        protected $bindings = [];
    
        /**
         * The container's method bindings.
         * 容器的方法绑定。
         *
         * @var array
         */
        protected $methodBindings = [];
    
        /**
         * The container's shared instances.
         * 容器的共享实例。
         *
         * @var array
         */
        protected $instances = [];
    
        /**
         * The registered type aliases.
         * 注册的类型别名。
         *
         * @var array
         */
        protected $aliases = [];
    
        /**
         * The registered aliases keyed by the abstract name.
         * 以抽象名称为关键字的注册别名。
         *
         * @var array
         */
        protected $abstractAliases = [];
    
        /**
         * The extension closures for services.
         * 服务的扩展名闭包。
         *
         * @var array
         */
        protected $extenders = [];
    
        /**
         * All of the registered tags.
         * 所有注册的标签。
         *
         * @var array
         */
        protected $tags = [];
    
        /**
         * The stack of concretions currently being built.
         * 目前正在构建的堆栈。
         *
         * @var array
         */
        protected $buildStack = [];
    
        /**
         * The parameter override stack.
         * 参数覆盖堆栈。
         *
         * @var array
         */
        protected $with = [];
    
        /**
         * The contextual binding map.
         * 上下文绑定图。
         *
         * @var array
         */
        public $contextual = [];
    
        /**
         * All of the registered rebound callbacks.
         * 所有已注册的反弹回调。
         *
         * @var array
         */
        protected $reboundCallbacks = [];
    
        /**
         * All of the global resolving callbacks.
         * 所有全局解析回调。
         *
         * @var array
         */
        protected $globalResolvingCallbacks = [];
    
        /**
         * All of the global after resolving callbacks.
         * 解决回调后的所有全局变量。
         *
         * @var array
         */
        protected $globalAfterResolvingCallbacks = [];
    
        /**
         * All of the resolving callbacks by class type.
         * 按类类型的所有解析回调。
         *
         * @var array
         */
        protected $resolvingCallbacks = [];
    
        /**
         * All of the after resolving callbacks by class type.
         * 所有按类类型解析后的回调。
         *
         * @var array
         */
        protected $afterResolvingCallbacks = [];
    
        /**
         * Define a contextual binding.
         * 定义上下文绑定。
         *
         * @param  string  $concrete
         * @return \Guanguans\Di\ContextualBindingBuilder
         */
        public function when($concrete)
        {
            return new ContextualBindingBuilder($this, $this->getAlias($concrete));
        }
    
        /**
         * Determine if the given abstract type has been bound.
         * 确定给定的抽象类型是否已绑定。
         *
         * @param  string  $abstract
         * @return bool
         */
        public function bound($abstract)
        {
            return isset($this->bindings[$abstract]) ||
                   isset($this->instances[$abstract]) ||
                   $this->isAlias($abstract);
        }
    
        /**
         * Determine if the given abstract type has been resolved.
         * 确定给定的抽象类型是否已解析。
         *
         * @param  string  $abstract
         * @return bool
         */
        public function resolved($abstract)
        {
            if ($this->isAlias($abstract)) {
                $abstract = $this->getAlias($abstract);
            }
    
            return isset($this->resolved[$abstract]) ||
                   isset($this->instances[$abstract]);
        }
    
        /**
         * Determine if a given type is shared.
         * 确定给定类型是否共享。
         *
         * @param  string  $abstract
         * @return bool
         */
        public function isShared($abstract)
        {
            return isset($this->instances[$abstract]) ||
                  (isset($this->bindings[$abstract]['shared']) &&
                   $this->bindings[$abstract]['shared'] === true);
        }
    
        /**
         * Determine if a given string is an alias.
         * 确定给定的字符串是否为别名
         *
         * @param  string  $name
         * @return bool
         */
        public function isAlias($name)
        {
            return isset($this->aliases[$name]);
        }
    
        /**
         * Register a binding with the container.
         * 在容器中注册绑定
         *
         * @param  string|array  $abstract
         * @param  \Closure|string|null  $concrete
         * @param  bool  $shared
         * @return void
         */
        public function bind($abstract, $concrete = null, $shared = false)
        {
            // If no concrete type was given, we will simply set the concrete type to the
            // abstract type. After that, the concrete type to be registered as shared
            // without being forced to state their classes in both of the parameters.
            // 如果没有给出具体类型,我们将简单地将具体类型设置为抽象类型。
            // 之后,要注册为共享的具体类型,而不必强制在两个参数中声明其类。
            $this->dropStaleInstances($abstract);
    
            if (is_null($concrete)) {
                $concrete = $abstract;
            }
    
            // If the factory is not a Closure, it means it is just a class name which is
            // bound into this container to the abstract type and we will just wrap it
            // up inside its own Closure to give us more convenience when extending.
            // 如果工厂不是 Closure,则意味着它只是一个类名称,
            // 该类名称绑定到此容器中,与抽象类型绑定,
            // 我们将其包装在自己的 Closure 中,以便在扩展时为我们提供更多便利。
            if (! $concrete instanceof Closure) {
                $concrete = $this->getClosure($abstract, $concrete);
            }
    
            $this->bindings[$abstract] = compact('concrete', 'shared');
    
            // If the abstract type was already resolved in this container we'll fire the
            // rebound listener so that any objects which have already gotten resolved
            // can have their copy of the object updated via the listener callbacks.
            // 如果抽象类型已在此容器中解析,我们将触发回弹侦听器,
            // 以便所有已解析的对象都可以通过侦听器回调更新其对象副本。
            if ($this->resolved($abstract)) {
                $this->rebound($abstract);
            }
        }
    
        /**
         * Get the Closure to be used when building a type.
         * 获取要在构建类型时使用的闭包。
         *
         * @param  string  $abstract
         * @param  string  $concrete
         * @return \Closure
         */
        protected function getClosure($abstract, $concrete)
        {
            return function ($container, $parameters = []) use ($abstract, $concrete) {
                if ($abstract == $concrete) {
                    return $container->build($concrete);
                }
    
                return $container->makeWith($concrete, $parameters);
            };
        }
    
        /**
         * Determine if the container has a method binding.
         * 确定容器是否具有方法绑定。
         *
         * @param  string  $method
         * @return bool
         */
        public function hasMethodBinding($method)
        {
            return isset($this->methodBindings[$method]);
        }
    
        /**
         * Bind a callback to resolve with Container::call.
         * 绑定回调以使用 Container::call 进行解析。
         *
         * @param  string  $method
         * @param  \Closure  $callback
         * @return void
         */
        public function bindMethod($method, $callback)
        {
            $this->methodBindings[$method] = $callback;
        }
    
        /**
         * Get the method binding for the given method.
         * 获取给定方法的方法绑定。
         *
         * @param  string  $method
         * @param  mixed  $instance
         * @return mixed
         */
        public function callMethodBinding($method, $instance)
        {
            return call_user_func($this->methodBindings[$method], $instance, $this);
        }
    
        /**
         * Add a contextual binding to the container.
         * 将上下文绑定添加到容器。
         *
         * @param  string  $concrete
         * @param  string  $abstract
         * @param  \Closure|string  $implementation
         * @return void
         */
        public function addContextualBinding($concrete, $abstract, $implementation)
        {
            $this->contextual[$concrete][$this->getAlias($abstract)] = $implementation;
        }
    
        /**
         * Register a binding if it hasn't already been registered.
         * 注册绑定(如果尚未注册)。
         *
         * @param  string  $abstract
         * @param  \Closure|string|null  $concrete
         * @param  bool  $shared
         * @return void
         */
        public function bindIf($abstract, $concrete = null, $shared = false)
        {
            if (! $this->bound($abstract)) {
                $this->bind($abstract, $concrete, $shared);
            }
        }
    
        /**
         * Register a shared binding in the container.
         * 在容器中注册共享绑定。
         *
         * @param  string|array  $abstract
         * @param  \Closure|string|null  $concrete
         * @return void
         */
        public function singleton($abstract, $concrete = null)
        {
            $this->bind($abstract, $concrete, true);
        }
    
        /**
         * "Extend" an abstract type in the container.
         * “扩展”容器中的抽象类型。
         *
         * @param  string    $abstract
         * @param  \Closure  $closure
         * @return void
         *
         * @throws \InvalidArgumentException
         */
        public function extend($abstract, Closure $closure)
        {
            $abstract = $this->getAlias($abstract);
    
            if (isset($this->instances[$abstract])) {
                $this->instances[$abstract] = $closure($this->instances[$abstract], $this);
    
                $this->rebound($abstract);
            } else {
                $this->extenders[$abstract][] = $closure;
    
                if ($this->resolved($abstract)) {
                    $this->rebound($abstract);
                }
            }
        }
    
        /**
         * Register an existing instance as shared in the container.
         * 将现有实例注册为在容器中共享的实例。
         *
         * @param  string  $abstract
         * @param  mixed   $instance
         * @return void
         */
        public function instance($abstract, $instance)
        {
            $this->removeAbstractAlias($abstract);
    
            $isBound = $this->bound($abstract);
    
            unset($this->aliases[$abstract]);
    
            // We'll check to determine if this type has been bound before, and if it has
            // we will fire the rebound callbacks registered with the container and it
            // can be updated with consuming classes that have gotten resolved here.
            // 我们将检查以确定此类型是否以前已绑定,
            // 如果已经绑定,将触发在容器中注册的反弹回调,
            // 并且可以使用在这里已解决的消耗类进行更新。
            $this->instances[$abstract] = $instance;
    
            if ($isBound) {
                $this->rebound($abstract);
            }
        }
    
        /**
         * Remove an alias from the contextual binding alias cache.
         * 从上下文绑定别名缓存中删除别名。
         *
         * @param  string  $searched
         * @return void
         */
        protected function removeAbstractAlias($searched)
        {
            if (! isset($this->aliases[$searched])) {
                return;
            }
    
            foreach ($this->abstractAliases as $abstract => $aliases) {
                foreach ($aliases as $index => $alias) {
                    if ($alias == $searched) {
                        unset($this->abstractAliases[$abstract][$index]);
                    }
                }
            }
        }
    
        /**
         * Assign a set of tags to a given binding.
         * 将一组标签分配给给定的绑定。
         *
         * @param  array|string  $abstracts
         * @param  array|mixed   ...$tags
         * @return void
         */
        public function tag($abstracts, $tags)
        {
            $tags = is_array($tags) ? $tags : array_slice(func_get_args(), 1);
    
            foreach ($tags as $tag) {
                if (! isset($this->tags[$tag])) {
                    $this->tags[$tag] = [];
                }
    
                foreach ((array) $abstracts as $abstract) {
                    $this->tags[$tag][] = $abstract;
                }
            }
        }
    
        /**
         * Resolve all of the bindings for a given tag.
         * 解决给定标签的所有绑定。
         *
         * @param  string  $tag
         * @return array
         */
        public function tagged($tag)
        {
            $results = [];
    
            if (isset($this->tags[$tag])) {
                foreach ($this->tags[$tag] as $abstract) {
                    $results[] = $this->make($abstract);
                }
            }
    
            return $results;
        }
    
        /**
         * Alias a type to a different name.
         * 将别名别名为另一个名称。
         *
         * @param  string  $abstract
         * @param  string  $alias
         * @return void
         */
        public function alias($abstract, $alias)
        {
            $this->aliases[$alias] = $abstract;
    
            $this->abstractAliases[$abstract][] = $alias;
        }
    
        /**
         * Bind a new callback to an abstract's rebind event.
         * 绑定一个新的回调到抽象的重新绑定事件。
         *
         * @param  string    $abstract
         * @param  \Closure  $callback
         * @return mixed
         */
        public function rebinding($abstract, Closure $callback)
        {
            $this->reboundCallbacks[$abstract = $this->getAlias($abstract)][] = $callback;
    
            if ($this->bound($abstract)) {
                return $this->make($abstract);
            }
        }
    
        /**
         * Refresh an instance on the given target and method.
         * 在给定的目标和方法上刷新实例。
         *
         * @param  string  $abstract
         * @param  mixed   $target
         * @param  string  $method
         * @return mixed
         */
        public function refresh($abstract, $target, $method)
        {
            return $this->rebinding($abstract, function ($app, $instance) use ($target, $method) {
                $target->{$method}($instance);
            });
        }
    
        /**
         * Fire the "rebound" callbacks for the given abstract type.
         * 为给定的抽象类型触发“反弹”回调。
         *
         * @param  string  $abstract
         * @return void
         */
        protected function rebound($abstract)
        {
            $instance = $this->make($abstract);
    
            foreach ($this->getReboundCallbacks($abstract) as $callback) {
                call_user_func($callback, $this, $instance);
            }
        }
    
        /**
         * Get the rebound callbacks for a given type.
         * 获取给定类型的反弹回调
         *
         * @param  string  $abstract
         * @return array
         */
        protected function getReboundCallbacks($abstract)
        {
            if (isset($this->reboundCallbacks[$abstract])) {
                return $this->reboundCallbacks[$abstract];
            }
    
            return [];
        }
    
        /**
         * Wrap the given closure such that its dependencies will be injected when executed.
         * 包装给定的闭包,以便在执行时注入其依赖项。
         *
         * @param  \Closure  $callback
         * @param  array  $parameters
         * @return \Closure
         */
        public function wrap(Closure $callback, array $parameters = [])
        {
            return function () use ($callback, $parameters) {
                return $this->call($callback, $parameters);
            };
        }
    
        /**
         * Call the given Closure / class@method and inject its dependencies.
         * 调用给定的 Closure / class@method 并注入其依赖项。
         *
         * @param  callable|string  $callback
         * @param  array  $parameters
         * @param  string|null  $defaultMethod
         * @return mixed
         */
        public function call($callback, array $parameters = [], $defaultMethod = null)
        {
            return BoundMethod::call($this, $callback, $parameters, $defaultMethod);
        }
    
        /**
         * Get a closure to resolve the given type from the container.
         * 获取一个容器来解析给定类型的闭包。
         *
         * @param  string  $abstract
         * @return \Closure
         */
        public function factory($abstract)
        {
            return function () use ($abstract) {
                return $this->make($abstract);
            };
        }
    
        /**
         * Resolve the given type with the given parameter overrides.
         * 使用给定的参数覆盖解析给定的类型。
         *
         * @param  string  $abstract
         * @param  array  $parameters
         * @return mixed
         */
        public function makeWith($abstract, array $parameters)
        {
            return $this->resolve($abstract, $parameters);
        }
    
        /**
         * Resolve the given type from the container.
         * 从容器解析给定类型。
         *
         * @param  string  $abstract
         * @return mixed
         */
        public function make($abstract)
        {
            return $this->resolve($abstract);
        }
    
        /**
         * Resolve the given type from the container.
         * 从容器解析给定类型。
         *
         * @param  string  $abstract
         * @param  array  $parameters
         * @return mixed
         */
        protected function resolve($abstract, $parameters = [])
        {
            $abstract = $this->getAlias($abstract);
    
            $needsContextualBuild = ! empty($parameters) || ! is_null(
                $this->getContextualConcrete($abstract)
            );
    
            // If an instance of the type is currently being managed as a singleton we'll
            // just return an existing instance instead of instantiating new instances
            // so the developer can keep using the same objects instance every time.
            // 如果当前正在以单例方式管理该类型的实例,
            // 我们将只返回一个现有实例,而不是实例化新实例,
            // 以便开发人员每次都可以继续使用相同的对象实例。
            if (isset($this->instances[$abstract]) && ! $needsContextualBuild) {
                return $this->instances[$abstract];
            }
    
            $this->with[] = $parameters;
    
            $concrete = $this->getConcrete($abstract);
    
            // We're ready to instantiate an instance of the concrete type registered for
            // the binding. This will instantiate the types, as well as resolve any of
            // its "nested" dependencies recursively until all have gotten resolved.
            // 我们已经准备好实例化为绑定注册的具体类型的实例。
            // 这将实例化类型,并以递归方式解析其“嵌套”依赖项中的任何一个,直到全部解析完毕。
            if ($this->isBuildable($concrete, $abstract)) {
                $object = $this->build($concrete);
            } else {
                $object = $this->make($concrete);
            }
    
            // If we defined any extenders for this type, we'll need to spin through them
            // and apply them to the object being built. This allows for the extension
            // of services, such as changing configuration or decorating the object.
            // 如果我们为此类型定义了任何扩展程序,则需要遍历它们并将其应用于正在构建的对象。
            // 这允许扩展服务,例如更改配置或装饰对象。
            foreach ($this->getExtenders($abstract) as $extender) {
                $object = $extender($object, $this);
            }
    
            // If the requested type is registered as a singleton we'll want to cache off
            // the instances in "memory" so we can return it later without creating an
            // entirely new instance of an object on each subsequent request for it.
            // 如果将请求的类型注册为单例,我们将将实例缓存在“内存”中,
            // 这样​​我们以后就可以返回它,而无需在随后的每次请求上都创建对象的全新实例。
            if ($this->isShared($abstract) && ! $needsContextualBuild) {
                $this->instances[$abstract] = $object;
            }
    
            $this->fireResolvingCallbacks($abstract, $object);
    
            // Before returning, we will also set the resolved flag to "true" and pop off
            // the parameter overrides for this build. After those two things are done
            // we will be ready to return back the fully constructed class instance.
            // 在返回之前,我们还将解析标记设置为 “true”,并弹出此版本的参数替代。
            // 完成这两件事之后,我们将准备好返回完整构造的类实例。
            $this->resolved[$abstract] = true;
    
            array_pop($this->with);
    
            return $object;
        }
    
        /**
         * Get the concrete type for a given abstract.
         * 获取给定摘要的具体类型。
         *
         * @param  string  $abstract
         * @return mixed   $concrete
         */
        protected function getConcrete($abstract)
        {
            if (! is_null($concrete = $this->getContextualConcrete($abstract))) {
                return $concrete;
            }
    
            // If we don't have a registered resolver or concrete for the type, we'll just
            // assume each type is a concrete name and will attempt to resolve it as is
            // since the container should be able to resolve concretes automatically.
            // 如果我们没有该类型的注册解析器或具体对象,我们将假设每种类型都是具体名称,
            // 并尝试按原样解析它,因为容器应该能够自动解析具体对象。
            if (isset($this->bindings[$abstract])) {
                return $this->bindings[$abstract]['concrete'];
            }
    
            return $abstract;
        }
    
        /**
         * Get the contextual concrete binding for the given abstract.
         * 获取给定摘要的上下文具体绑定。
         *
         * @param  string  $abstract
         * @return string|null
         */
        protected function getContextualConcrete($abstract)
        {
            if (! is_null($binding = $this->findInContextualBindings($abstract))) {
                return $binding;
            }
    
            // Next we need to see if a contextual binding might be bound under an alias of the
            // given abstract type. So, we will need to check if any aliases exist with this
            // type and then spin through them and check for contextual bindings on these.
            // 接下来,我们需要查看是否可以在给定抽象类型的别名下绑定上下文绑定。
            // 因此,我们将需要检查此类型是否存在别名,然后旋转它们并检查这些别名的上下文绑定。
            if (empty($this->abstractAliases[$abstract])) {
                return;
            }
    
            foreach ($this->abstractAliases[$abstract] as $alias) {
                if (! is_null($binding = $this->findInContextualBindings($alias))) {
                    return $binding;
                }
            }
        }
    
        /**
         * Find the concrete binding for the given abstract in the contextual binding array.
         * 在上下文绑定数组中找到给定摘要的具体绑定
         *
         * @param  string  $abstract
         * @return string|null
         */
        protected function findInContextualBindings($abstract)
        {
            if (isset($this->contextual[end($this->buildStack)][$abstract])) {
                return $this->contextual[end($this->buildStack)][$abstract];
            }
        }
    
        /**
         * Determine if the given concrete is buildable.
         * 确定给定的具体是否可构建。
         *
         * @param  mixed   $concrete
         * @param  string  $abstract
         * @return bool
         */
        protected function isBuildable($concrete, $abstract)
        {
            return $concrete === $abstract || $concrete instanceof Closure;
        }
    
        /**
         * Instantiate a concrete instance of the given type.
         * 实例化给定类型的具体实例。
         *
         * @param  string  $concrete
         * @return mixed
         *
         * @throws \Guanguans\Di\BindingResolutionException
         */
        public function build($concrete)
        {
            // If the concrete type is actually a Closure, we will just execute it and
            // hand back the results of the functions, which allows functions to be
            // used as resolvers for more fine-tuned resolution of these objects.
            // 如果具体类型实际上是 Closure,我们将直接执行它并返回函数的结果,
            // 这允许将函数用作解析器,以便对这些对象进行更精细的解析。
            if ($concrete instanceof Closure) {
                return $concrete($this, $this->getLastParameterOverride());
            }
    
            $reflector = new ReflectionClass($concrete);
    
            // If the type is not instantiable, the developer is attempting to resolve
            // an abstract type such as an Interface of Abstract Class and there is
            // no binding registered for the abstractions so we need to bail out.
            // 如果类型不是可实例化的,则开发人员正尝试解析抽象类型,
            // 例如 Abstract Class 的接口,并且没有为抽象注册任何绑定,因此我们需要纾困。
            if (! $reflector->isInstantiable()) {
                return $this->notInstantiable($concrete);
            }
    
            $this->buildStack[] = $concrete;
    
            $constructor = $reflector->getConstructor();
    
            // If there are no constructors, that means there are no dependencies then
            // we can just resolve the instances of the objects right away, without
            // resolving any other types or dependencies out of these containers.
            // 如果没有构造函数,则意味着没有依赖项,我们可以立即解析对象的实例,
            // 而无需从这些容器中解析任何其他类型或依赖项。
            if (is_null($constructor)) {
                array_pop($this->buildStack);
    
                return new $concrete;
            }
    
            $dependencies = $constructor->getParameters();
    
            // Once we have all the constructor's parameters we can create each of the
            // dependency instances and then use the reflection instances to make a
            // new instance of this class, injecting the created dependencies in.
            // 一旦有了所有构造函数的参数,就可以创建每个依赖实例,
            // 然后使用反射实例创建此类的新实例,将创建的依赖注入其中。
            $instances = $this->resolveDependencies(
                $dependencies
            );
    
            array_pop($this->buildStack);
    
            return $reflector->newInstanceArgs($instances);
        }
    
        /**
         * Resolve all of the dependencies from the ReflectionParameters.
         * 从 ReflectionParameters 解析所有依赖项。
         *
         * @param  array  $dependencies
         * @return array
         */
        protected function resolveDependencies(array $dependencies)
        {
            $results = [];
    
            foreach ($dependencies as $dependency) {
                // If this dependency has a override for this particular build we will use
                // that instead as the value. Otherwise, we will continue with this run
                // of resolutions and let reflection attempt to determine the result.
                // 如果此依赖项对此特定构建具有替代,我们将使用作为值。
                // 否则,我们将继续执行的分辨率,并让反射尝试确定结果。
                if ($this->hasParameterOverride($dependency)) {
                    $results[] = $this->getParameterOverride($dependency);
    
                    continue;
                }
    
                // If the class is null, it means the dependency is a string or some other
                // primitive type which we can not resolve since it is not a class and
                // we will just bomb out with an error since we have no-where to go.
                // 如果该类为 null,则表示依赖项是字符串或其他类型的原始类型,
                // 由于它不是类,因此我们无法解析,并且由于没有位置,我们将抛出错误去。
                $results[] = is_null($class = $dependency->getClass())
                                ? $this->resolvePrimitive($dependency)
                                : $this->resolveClass($dependency);
            }
    
            return $results;
        }
    
        /**
         * Determine if the given dependency has a parameter override from makeWith.
         * 确定给定的依赖项是否具有来自 makeWith 的参数重写。
         *
         * @param  \ReflectionParameter  $dependency
         * @return bool
         */
        protected function hasParameterOverride($dependency)
        {
            return array_key_exists(
                $dependency->name,
                $this->getLastParameterOverride()
            );
        }
    
        /**
         * Get a parameter override for a dependency.
         * 获取依赖项的参数覆盖。
         *
         * @param  \ReflectionParameter  $dependency
         * @return mixed
         */
        protected function getParameterOverride($dependency)
        {
            return $this->getLastParameterOverride()[$dependency->name];
        }
    
        /**
         * Get the last parameter override.
         * 获取最后一个参数覆盖。
         *
         * @return array
         */
        protected function getLastParameterOverride()
        {
            return count($this->with) ? end($this->with) : [];
        }
    
        /**
         * Resolve a non-class hinted primitive dependency.
         * 解析非类提示的原始依赖项。
         *
         * @param  \ReflectionParameter  $parameter
         * @return mixed
         *
         * @throws \Guanguans\Di\BindingResolutionException
         */
        protected function resolvePrimitive(ReflectionParameter $parameter)
        {
            if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
                return $concrete instanceof Closure ? $concrete($this) : $concrete;
            }
    
            if ($parameter->isDefaultValueAvailable()) {
                return $parameter->getDefaultValue();
            }
    
            $this->unresolvablePrimitive($parameter);
        }
    
        /**
         * Resolve a class based dependency from the container.
         * 解析容器中基于类的依赖关系。
         *
         * @param  \ReflectionParameter  $parameter
         * @return mixed
         *
         * @throws \Guanguans\Di\BindingResolutionException
         */
        protected function resolveClass(ReflectionParameter $parameter)
        {
            try {
                return $this->make($parameter->getClass()->name);
            }
    
            // If we can not resolve the class instance, we will check to see if the value
            // is optional, and if it is we will return the optional parameter value as
            // the value of the dependency, similarly to how we do this with scalars.
            // 如果无法解析类实例,则将检查该值是否为可选,如果为可选,
            // 们将返回可选参数 value 作为依赖项的值,类似于执行此操作的方式标量。
            catch (BindingResolutionException $e) {
                if ($parameter->isOptional()) {
                    return $parameter->getDefaultValue();
                }
    
                throw $e;
            }
        }
    
        /**
         * Throw an exception that the concrete is not instantiable.
         * 抛出一个具体无法实例化的异常。
         *
         * @param  string  $concrete
         * @return void
         *
         * @throws \Guanguans\Di\BindingResolutionException
         */
        protected function notInstantiable($concrete)
        {
            if (! empty($this->buildStack)) {
                $previous = implode(', ', $this->buildStack);
    
                $message = "Target [$concrete] is not instantiable while building [$previous].";
            } else {
                $message = "Target [$concrete] is not instantiable.";
            }
    
            throw new BindingResolutionException($message);
        }
    
        /**
         * Throw an exception for an unresolvable primitive.
         * 为无法解析的原语抛出异常。
         *
         * @param  \ReflectionParameter  $parameter
         * @return void
         *
         * @throws \Guanguans\Di\BindingResolutionException
         */
        protected function unresolvablePrimitive(ReflectionParameter $parameter)
        {
            $message = "Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}";
    
            throw new BindingResolutionException($message);
        }
    
        /**
         * Register a new resolving callback.
         * 注册一个新的解析回调。
         *
         * @param  string    $abstract
         * @param  \Closure|null  $callback
         * @return void
         */
        public function resolving($abstract, Closure $callback = null)
        {
            if (is_string($abstract)) {
                $abstract = $this->getAlias($abstract);
            }
    
            if (is_null($callback) && $abstract instanceof Closure) {
                $this->globalResolvingCallbacks[] = $abstract;
            } else {
                $this->resolvingCallbacks[$abstract][] = $callback;
            }
        }
    
        /**
         * Register a new after resolving callback for all types.
         * 在解析所有类型的回调之后注册一个新的。
         *
         * @param  string   $abstract
         * @param  \Closure|null $callback
         * @return void
         */
        public function afterResolving($abstract, Closure $callback = null)
        {
            if (is_string($abstract)) {
                $abstract = $this->getAlias($abstract);
            }
    
            if ($abstract instanceof Closure && is_null($callback)) {
                $this->globalAfterResolvingCallbacks[] = $abstract;
            } else {
                $this->afterResolvingCallbacks[$abstract][] = $callback;
            }
        }
    
        /**
         * Fire all of the resolving callbacks.
         * 触发所有解析的回调。
         *
         * @param  string  $abstract
         * @param  mixed   $object
         * @return void
         */
        protected function fireResolvingCallbacks($abstract, $object)
        {
            $this->fireCallbackArray($object, $this->globalResolvingCallbacks);
    
            $this->fireCallbackArray(
                $object,
                $this->getCallbacksForType($abstract, $object, $this->resolvingCallbacks)
            );
    
            $this->fireAfterResolvingCallbacks($abstract, $object);
        }
    
        /**
         * Fire all of the after resolving callbacks.
         * 解决回调后,请执行所有操作。
         *
         * @param  string  $abstract
         * @param  mixed   $object
         * @return void
         */
        protected function fireAfterResolvingCallbacks($abstract, $object)
        {
            $this->fireCallbackArray($object, $this->globalAfterResolvingCallbacks);
    
            $this->fireCallbackArray(
                $object,
                $this->getCallbacksForType($abstract, $object, $this->afterResolvingCallbacks)
            );
        }
    
        /**
         * Get all callbacks for a given type.
         * 获取给定类型的所有回调。
         *
         * @param  string  $abstract
         * @param  object  $object
         * @param  array   $callbacksPerType
         *
         * @return array
         */
        protected function getCallbacksForType($abstract, $object, array $callbacksPerType)
        {
            $results = [];
    
            foreach ($callbacksPerType as $type => $callbacks) {
                if ($type === $abstract || $object instanceof $type) {
                    $results = array_merge($results, $callbacks);
                }
            }
    
            return $results;
        }
    
        /**
         * Fire an array of callbacks with an object.
         * 触发带有对象的回调数组。
         *
         * @param  mixed  $object
         * @param  array  $callbacks
         * @return void
         */
        protected function fireCallbackArray($object, array $callbacks)
        {
            foreach ($callbacks as $callback) {
                $callback($object, $this);
            }
        }
    
        /**
         * Get the container's bindings.
         * 获取容器的绑定。
         *
         * @return array
         */
        public function getBindings()
        {
            return $this->bindings;
        }
    
        /**
         * Get the alias for an abstract if available.
         * 获取摘要的别名(如果有)。
         *
         * @param  string  $abstract
         * @return string
         *
         * @throws \LogicException
         */
        public function getAlias($abstract)
        {
            if (! isset($this->aliases[$abstract])) {
                return $abstract;
            }
    
            if ($this->aliases[$abstract] === $abstract) {
                throw new LogicException("[{$abstract}] is aliased to itself.");
            }
    
            return $this->getAlias($this->aliases[$abstract]);
        }
    
        /**
         * Get the extender callbacks for a given type.
         * 获取给定类型的扩展程序回调。
         *
         * @param  string  $abstract
         * @return array
         */
        protected function getExtenders($abstract)
        {
            $abstract = $this->getAlias($abstract);
    
            if (isset($this->extenders[$abstract])) {
                return $this->extenders[$abstract];
            }
    
            return [];
        }
    
        /**
         * Remove all of the extender callbacks for a given type.
         * 删除给定类型的所有扩展器回调。
         *
         * @param  string  $abstract
         * @return void
         */
        public function forgetExtenders($abstract)
        {
            unset($this->extenders[$this->getAlias($abstract)]);
        }
    
        /**
         * Drop all of the stale instances and aliases.
         * 删除所有过时的实例和别名。
         *
         * @param  string  $abstract
         * @return void
         */
        protected function dropStaleInstances($abstract)
        {
            unset($this->instances[$abstract], $this->aliases[$abstract]);
        }
    
        /**
         * Remove a resolved instance from the instance cache.
         * 从实例缓存中删除已解析的实例。
         *
         * @param  string  $abstract
         * @return void
         */
        public function forgetInstance($abstract)
        {
            unset($this->instances[$abstract]);
        }
    
        /**
         * Clear all of the instances from the container.
         * 从容器中清除所有实例。
         *
         * @return void
         */
        public function forgetInstances()
        {
            $this->instances = [];
        }
    
        /**
         * Flush the container of all bindings and resolved instances.
         * 刷新容器中的所有绑定和已解决的实例。
         *
         * @return void
         */
        public function flush()
        {
            $this->aliases = [];
            $this->resolved = [];
            $this->bindings = [];
            $this->instances = [];
            $this->abstractAliases = [];
        }
    
        /**
         * Set the globally available instance of the container.
         * 设置容器的全局可用实例。
         *
         * @return static
         */
        public static function getInstance()
        {
            if (is_null(static::$instance)) {
                static::$instance = new static;
            }
    
            return static::$instance;
        }
    
        /**
         * Set the shared instance of the container.
         * 设置容器的共享实例。
         *
         * @param  \Guanguans\Di\Container|null  $container
         * @return static
         */
        public static function setInstance(Container $container = null)
        {
            return static::$instance = $container;
        }
    
        /**
         * Determine if a given offset exists.
         * 确定是否存在给定的 key 。
         *
         * @param  string  $key
         * @return bool
         */
        public function offsetExists($key)
        {
            return $this->bound($key);
        }
    
        /**
         * Get the value at a given offset.
         * 获取给定 key 的值。
         *
         * @param  string  $key
         * @return mixed
         */
        public function offsetGet($key)
        {
            return $this->make($key);
        }
    
        /**
         * Set the value at a given offset.
         * 将值设置为给定的 key 。
         *
         * @param  string  $key
         * @param  mixed   $value
         * @return void
         */
        public function offsetSet($key, $value)
        {
            $this->bind($key, $value instanceof Closure ? $value : function () use ($value) {
                return $value;
            });
        }
    
        /**
         * Unset the value at a given offset.
         * 以给定 key 取消设置值。
         *
         * @param  string  $key
         * @return void
         */
        public function offsetUnset($key)
        {
            unset($this->bindings[$key], $this->instances[$key], $this->resolved[$key]);
        }
    
        /**
         * Dynamically access container services.
         * 动态访问容器服务。
         *
         * @param  string  $key
         * @return mixed
         */
        public function __get($key)
        {
            return $this[$key];
        }
    
        /**
         * Dynamically set container services.
         * 动态设置容器服务。
         *
         * @param  string  $key
         * @param  mixed   $value
         * @return void
         */
        public function __set($key, $value)
        {
            $this[$key] = $value;
        }
    
        /**
         * 通过其标识符查找容器的条目并返回它。
         *
         * @inheritDoc
         */
        public function has($id)
        {
            return $this->offsetExists($id);
        }
    
        /**
         * 如果容器可以返回给定标识符的条目,则返回 true 。 否则返回 false 。
         *
         * @inheritDoc
         */
        public function get($id)
        {
            return $this[$id];
        }
    }
    
    展开全文
  • 经过这段时间的积累我觉得我可以比较清晰的理顺laravel的ioc容器的基本结构和基本逻辑,跟大家分享一下。说一句题外话,万法一归途。什么意思呢,无论你学什么语言,到一定深度都会到达同一个终点。容器核心类:...
  • 理解服务容器的概念,对于我们使用laravel太重要了,应该说是否理解服务容器的概念是区分是否入门laravel的重要条件。因为整个框架正是在服务容器这一基础上构建起来的。 laravel服务容器就像一个高度自动化的工厂,...
  • 理解服务容器的概念,对于我们使用laravel太重要了,应该说是否理解服务容器的概念是区分是否入门laravel的重要条件。因为整个框架正是在服务容器这一基础上构建起来的。 laravel服务容器就像一个高度自动化的...
  • 阅读目录 1. laravel容器基本认识 2. 如何在代码中获取到容器实例 ...本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点。文章很长,但是内容应该很有用,希望有需要的朋友能看...
  • 理解服务容器的概念,对于我们使用laravel太重要了,应该说是否理解服务容器的概念是区分是否入门laravel的重要条件。因为整个框架正是在服务容器这一基础上构建起来的。laravel服务容器就像一个高度自动化的工厂,...
  • GitHub源码地址 摘取的Laravel框架中,Container的实现代码,并参考Laravel中关于容器的使用手册,写的一个容器使用示例。 详情请移步:GitHub
  • 理解服务容器的概念,对于我们使用laravel太重要了,应该说是否理解服务容器的概念是区分是否入门laravel的重要条件。因为整个框架正是在服务容器这一基础上构建起来的。laravel服务容器就像一个高度自动化的工厂,...
  • 主要给大家介绍了关于Laravel服务容器绑定几种方式,文中通过示例代码介绍非常详细,对大家学习或者使用Laravel具有一定参考学习价值,需要朋友们下面来一起学习学习吧
  • Laravel通过减轻许多Web项目中使用的常见任务来减轻开发工作痛苦,例如: 。 。 用于和存储多个后端。 直观表达 。 数据库不可知。 。 。 Laravel易于访问,功能强大,并提供大型,强大应用程序所需...
  • laravel 服务容器

    2020-07-21 16:36:32
    因为使用了服务容器,laravel中大部分对象实例化的...但是在没有使用服务容器的情况下,以下这种方式同样可以做到: $obj1 = new class1(new class2()); $obj2 = new class3(new class4()); 那么使用服务容器...
  • 阅读目录 1. laravel容器基本认识 ...3. 直观认识laravel容器 4. 如何理解服务绑定与解析 5. 服务提供者作用与使用 6. 服务绑定名称别名 7. 依赖注入机制 8. 其它 本文面向php语言larave...
  • laravel IoC容器心得

    千次阅读 2018-01-24 13:33:52
    前言 laravel现在已经是一个非常成熟PHP框架,在学习laravel的这一段时间内,laravel...在刚刚开始使用laravel的时候,说实话,阅读laravel文档有关服务容器和服务提供者章节时候是很迷茫。由于其中涉及了很多

空空如也

空空如也

1 2 3 4 5 ... 16
收藏数 308
精华内容 123
关键字:

laravel容器的使用