From Apprentice To Artisan 翻译 04

Reflect Resolution 反射解决方案

One of the most powerful features of the Laravel container is its ability to automatically resolve dependencies via reflection. Reflection is the ability to inspect a classes and methods. For example, the PHP ReflectionClass class allows you to inspect the method avaliable on a given class. The PHP method method_exists is also a form of reflection. To play with PHP‘s reflection class, try the followring code on one of your classes:

用反射来自动处理依赖是Laravel容器的一个最强大的特性。反射是一种运行时探测类和方法的能力。比如,PHP的ReflectionClass可以探测一个类的方法。method_exists某种意义上说也是一种反射。我们来把玩一下PHP的反射类,试试下面的代码吧(StripeBiller换成你自己定义好的类):

$reflection = new ReflectionClass(‘StripeBiller‘);
var_dump($reflection->getMethods());
var_dump($reflection->getConstants());

By leveraging this powerful feature of PHP, the Laravel IoC container can do some interesting things! For instance, consider the following class:

依靠这个强大的PHP特性, Laravel的IoC容器可以实现很有趣的功能!考虑接下来这个类:

class UserController extends BaseController
{
    public function __construct(StripBiller $biller)
    {
        $this->biller = $biller;
    }
}

Note that the controller is type-hinting the StripBiller class. We are able to retrieve this type-hint using reflection. When the Laravel container does not have a resolver for a class explictity bound, it will try to resolve the class via reflection. The flow looks like this:

注意这个控制器的构造函数暗示着有一个StripBiller类型的参数。使用反射就可以检测到这种类型暗示。当Laravel的容器无法解决一个类型的明显绑定时,容器会试着使用反射来解决。程序流程类似于这样的:

  1. Do I have a resolver for StripBiller?

    已经有一个StripBiller的绑定了么?

  2. No resolver? Reflect into StripBiller to determin if it has dependencies.

    没绑定?那用反射来探测一下StripBiller吧。看看他都需要什么依赖。

  3. Resolve any dependencies needed by StripBiller (recursive).

    解决StripBiller需要的所有依赖(递归处理)

  4. Instantiate new StripBiller instance via ReflectionClass->newInstanceArgs().

    使用ReflectionClass->newInstanceArgs()来实例化StripBiller

As you can see, the container is doing a lot of heavy lifting for you, which saves you from having to write resolves for every single one of your classes. This is one of the most powerful and unique features of the Laravel container, and having a strong grasp of this capability is very beneficial when building large Laravel applications.

如你所见, 容器替我们做了好多重活,这能帮你省去写大量绑定的麻烦。这就是Laravel容器最强大也是最独特的特性。熟练掌握这种能力对构建大型Laravel应用是十分有益的。

Now, let‘s modify our controller a bit. What if it looks like this?

下面我们修改一下控制器, 改成这样会发生什么事儿呢?

class UserController extends BaseController
{
    public function __construct(BillerInterface $biller)
    {
        $this->biller = $biller;
    }
}

Assuming we have not explicitly bound a resolver for BillerInterface, how will the container know what class to inject? Remember, interface can‘t be instantiated since they are just contracts. Without us giving the container any more information, it will be unable to instantiate this dependency. We need to specify a class that should be used as the default implementation of this interface, and we may do so via the bind method:

假设我们没有为BillerInterface做任何绑定, 容器该怎么知道要注入什么类呢?要知道,interface不能被实例化,因为它只是个约定。如果我们不提供更多信息的话,容器是无法实例化这个依赖的。我们需要明确指出哪个类要实现这个接口,这就需要用到bind方法:

App::bind(‘BillerInterface‘,‘StripBiller‘);

Here, we are passing a string instead of a Closure, and this string tells the container to always use the StripBiller class anytime it needs an implementation of the BillerInterface interface. Again, we‘re gaining the ability to switch implementations of services with a simple one-line change to our container binding. For example, if we need to switch to Balanced Payments as our billing provider, we simply write a new BalancedBiller implementation of BillerInterface, and change our container binding:

这里我们只传了一个字符串进去,而不是一个匿名函数。 这个字符串告诉容器总是使用StripBiller来作为BillerInterface的实现类。 此外我们也获得了只改一行代码即可轻松改变实现的能力。比如,假设我们需要切换到Balanced Payments作为我们的支付提供商,我们只需要新写一个BalancedBiller来实现BillerInterface接口,然后这样修改容器代码:

App::bind(‘BillerInterface‘, ‘BalancedBiller‘);

Automatically, our new implementation will be used throughout out application!

我们的应用程序就装载上了的新支付实现代码了!

When binding implementations to interfaces, you may also use the singleton method so the container only instantiates one instance of the class per request cycle:

你也可以使用singleton方法来实现单例模式。

App::singleton(‘BillerInterface‘, ‘StripBiller‘);

Master The Container 掌握容器

Want to learn even more about the container? Read through its source! The container is only one class: Illuminate\Container\Container. Read over the source code to gain a deeper understanding of how the container works under the hood.

想了解更多关于容器的知识? 去读源码!容器只有一个类Illuminate\Container\Container. 读完了你就对容器有更深的认识了。

时间: 2024-10-29 10:48:41

From Apprentice To Artisan 翻译 04的相关文章

From Apprentice To Artisan 翻译 01

Dependency Injection 依赖注入 The Problem 遇到的问题 The foundation of the Laravel framework is its powerful IoC container. To truly understand the framework, a strong grasp of the container is necessary. However, we shold note that an IoC container is simply

From Apprentice To Artisan 翻译 03

The IoC Container 控制反转容器 Basic Binding 基础绑定 Now that we've learned about dependency injection, let's explore inversion of control containers. IoC containers make managing your class dependencies much more convenient, and Laravel ships with a very pow

From Apprentice To Artisan 翻译 02

Respect Boundaries 严守边界 Remember to respect responsibility boundaries. Controllers and routes serve as a mediator between HTTP and your application. When writing large applications, don't clutter them up with your domain logic. 记得要保持清晰的责任边界. 控制器和路由是作

6. Laravel5学习笔记:IOC/DI的理解

介绍 IOC 控制反转 Inversion of Control 依赖关系的转移 依赖抽象而非实践 DI 依赖注入 Dependency Injection 不必自己在代码中维护对象的依赖 容器自动根据配置,将依赖注入指定对象 IOC.DI对于Laravel的意义 Laravel框架的基础是一个功能强大的控制反转容器(IoC container). 为了真正理解该框架,需要好好掌握该容器.然而我们需要了解,控制反转容器只是一种用于方便实现"依赖注入"的工具.但要实现依赖注入并不一定需要

精体展矿计运界布属点世据真起验青kPswaoY3w

社保划到税务征收,将大大提升社保费的征管效率.税务的征管能力是目前而言最强的,以后税务征收社保不是代收,属于本职了. 之前税局要把社保信息和交个税的工资比对起来有困难!现在好了,个税是自己的,社保也是自己的,比对困难?不存在的! 这一变革,会给那些不给员工上社保.不全额上社保的企业致命一击! 最新案例 前段时间的发改委关于限制特定严重失信人乘坐民航的一则意见--发改财金[2018]385号,其中还有税务总局的联合署名. http://weibo.com/20180408PP/2309279811

而广除还状么林验以适调半去gbQwsadnbQjN

为了从不同环节,尤其与广大使用人群直接关系的环节反映质量状况,对共享自行车投放点.运营仓库.生产企业等不同环节的产品抽查,覆盖了共享自行车从成品出厂到待投放的关键环节. 该负责人称,根据新车投放情况,结合共享自行车行业市场占有分布特点,本次重点抽查了摩拜.ofo.Hellobike三个品牌的产品,占本次抽查批次总数的83.3%.其中,在天津.无锡.武汉.广州.深圳.东莞6个城市抽查了9批次摩拜产品,占产品抽查批次总数的37.5%,抽查批次合格率88.9%,抽查不合格的1批次产品为待投放于广州市的

04微信公众平台 - 实现【翻译】功能函数,返回一个文本字符串。

一.功能代码函数实现 private function _baiduDic($keyword) { $tranurlaip = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=9peNkh97N6B9GGj9zBke9tGQ&q={$keyword}&from=auto&to=auto"; //翻译URLapi地址 $transtr = file_get_contents($tranurl

转【翻译】如何在Ubuntu 12.04上配置Apache SSL证书

关于SSL证书 SSL证书是加密站点信息和创建一个更安全的连接的一种方式.另外,证书可以向站点访问者展示VPS的身份信息.证书颁发机构颁发SSL证书,用来验证服务器的详细信息,而一个自签名的证书缺乏第三方机构的证明. 设置 以下教程,需要拥有VPS上root权限. 另外,你的虚拟服务器上需要安装并运行有apache.如果没有安装,可以通过以下命令安装: sudo apt-get install apache2 第一步--启用SSL模块 下一步启用SSL sudo a2enmod ssl 紧接着重

修正Smart Install Maker 5.04 中文翻译导致检测.net版本出错的问题

Smart Install Maker 5.04 中文翻译导致检测.net版本出错问题,本人已修正,有需要的可以下载,替换“Language\Install\”目录下的中文翻译文件. Smart Install Maker 5.04 中文翻译修正版下载:占我下载 修正Smart Install Maker 5.04 中文翻译导致检测.net版本出错的问题