zoukankan      html  css  js  c++  java
  • Laravel之Service Container服务容器

    managing class dependencies and performing dependency injection.
    Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.
    Almost all of your service container bindings will be registered within service providers, so most of these examples will demonstrate using the container in that context.
    There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection.
    Simple Bindings
    Within a service provider, you always have access to the container via the $this->app property. We can register a binding using the bind method, passing the class or interface name that we wish to register along with a Closure that returns an instance of the class:
    $this->app->bind('HelpSpotAPI', function ($app) { return new HelpSpotAPI($app->make('HttpClient')); });
    Note that we receive the container itself as an argument to the resolver. We can then use the container to resolve sub-dependencies of the object we are building.
    Binding A Singleton 单例
    The singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:
    $this->app->singleton('HelpSpotAPI', function ($app) { return new HelpSpotAPI($app->make('HttpClient')); });
    Binding Instances
    You may also bind an existing object instance into the container using the instance method. The given instance will always be returned on subsequent calls into the container:
    $api = new HelpSpotAPI(new HttpClient); $this->app->instance('HelpSpotApi', $api);
    Binding Primitives绑定常用的基本类型
    Sometimes you may have a class that receives some injected classes, but also needs an injected primitive value such as an integer. You may easily use contextual binding to inject any value your class may need:
    $this->app->when('AppHttpControllersUserController') ->needs('$variableName') ->give($value);
    Binding Interfaces To Implementations
    $this->app->bind( 'AppContractsEventPusher', 'AppServicesRedisEventPusher' );
    Contextual Binding
    解析:
    You may use the make method to resolve a class instance out of the container. The make method accepts the name of the class or interface you wish to resolve:
    $api = $this->app->make('HelpSpotAPI');
    The service container fires an event each time it resolves an object. You may listen to this event using the resolving method:
    allowing you to set any additional properties on the object before it is given to its consumer.
  • 相关阅读:
    (AIDE)Android Eclipse JNI 调用 .so文件加载问题
    VMware10.06精简版安装后台运行
    RAID磁盘阵列笔记
    高科固定电话机铃声
    嵌入式Linux驱动学习之路(十八)LCD驱动
    嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动
    嵌入式Linux驱动学习之路(十六)输入子系统
    嵌入式Linux驱动学习之路(十五)按键驱动-定时器防抖
    嵌入式Linux驱动学习之路(十四)按键驱动-同步、互斥、阻塞
    嵌入式Linux驱动学习之路(十三)按键驱动-异步通知
  • 原文地址:https://www.cnblogs.com/elesos/p/5924670.html
Copyright © 2011-2022 走看看