文档章节

From Apprentice To Artisan 翻译 04

zgldh
 zgldh
发布于 2014/10/08 20:38
字数 1215
阅读 3344
收藏 2

上一篇

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换成你自己定义好的类):

<!-- lang: php -->
$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容器可以实现很有趣的功能!考虑接下来这个类:

<!-- lang:php -->
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?

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

<!-- lang:php -->
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方法:

<!-- lang:php -->
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接口,然后这样修改容器代码:

<!-- lang:php -->
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方法来实现单例模式。

<!-- lang:php -->
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. 读完了你就对容器有更深的认识了。

下一篇

© 著作权归作者所有

zgldh

zgldh

粉丝 102
博文 36
码字总数 47009
作品 2
高级程序员
私信 提问
From Apprentice To Artisan 翻译 16

上一篇 Single Responsibility Principle 单一职责原则 Introduction 介绍 The "SOLID" design principles, articulated by Robert "Uncle Bob" Martin, are five principles that provide ......

zgldh
2015/03/17
2.6K
0
From Apprentice To Artisan 翻译 10

上一篇 Application Structure 应用结构 Introduction 介绍 Where does this class belong? This question is extremely common when building applications on a framework. Many develope......

zgldh
2014/12/29
12.2K
1
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......

zgldh
2014/09/08
6.2K
0
From Apprentice To Artisan 翻译 20

上一篇 Dependency Inversion Principle 依赖反转原则 Introduction 介绍 We have reached our final destination in our overview of the five SOLID design principles! The final princip......

zgldh
2015/03/19
2.1K
5
From Apprentice To Artisan 翻译 17

上一篇 Open Closed Principle 开放封闭原则 Introduction 介绍 Over the life time of an application, more time is spent adding to the existing codebase rather than constantly addi......

zgldh
2015/03/18
2.1K
0

没有更多内容

加载失败,请刷新页面

加载更多

JDBC+C3P0+DBCP 基本使用

1.概述 这篇文章主要说了JDBC的基本使用,包括Statement,PreparedStatement,JDBC的连接,Mysql创建用户创建数据表,C3P0的连接与配置,DBCP的连接与配置. 2.mysql的处理 这里的JDBC使用Mysql作为...

Blueeeeeee
34分钟前
3
0
MVC Linux下开发及部署

linux使用的是 Ubuntu 64 位 18.04.2 LTS 首先复制C:\Program Files (x86)\Embarcadero\Studio\20.0\PAServer 下 LinuxPAServer20.0.tar.gz 到 linux 目录下 运行链接编译程序 delphi环境配置......

苏兴迎
今天
9
0
3.控件及其属性

1.文本 2.按钮

横着走的螃蟹
今天
7
0
安装Genymotion模拟器慢的解决方案

第一步点击下载, C:\Users\Administrator\AppData\Local\Genymobile\genymotion.log 中搜索 ova 会发现这个文件 使用迅雷下载即可. 在 虚拟机中导入这个.ova 文件 即可安装...

chenhongjiang
今天
5
0
4. 彤哥说netty系列之Java NIO实现群聊(自己跟自己聊上瘾了)

你好,我是彤哥,本篇是netty系列的第四篇。 欢迎来我的公从号彤哥读源码系统地学习源码&架构的知识。 简介 上一章我们一起学习了Java中的BIO/NIO/AIO的故事,本章将带着大家一起使用纯纯的N...

彤哥读源码
今天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部