Laravel 4 - 容器类:共享功能 &闭合逻辑 [英] Laravel 4 - Container class: share function & closure logic

查看:17
本文介绍了Laravel 4 - 容器类:共享功能 &闭合逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此处讨论的问题,我有一个后续问题:Laravel 核心方法混淆

I have a follow-up question to the one discussed here: Laravel core method confusion

我和driechel(上述问题的作者)之前的情况相同,目前正在习惯 Laravel 4 FW 并检查内核.虽然已经给出了准确的答案,但我仍然不明白其中的逻辑和幕后发生的事情.所以我非常感谢进一步的解释.我知道这可能是重复的,但由于我还不能发表评论,我会用一个新问题试一试.希望这样没问题.

I am in the same situation as driechel (author of question above) has been before, currently getting used to Laravel 4 FW and examining the core. Although a precise answer has been given I still don't understand the logic and what is happening under the hood. So I would very much appreciate a further explanation. I know this might be a duplicate but since I cannot post comments yet I'll give it a shot with a new question. Hope it' ok this way.

从这篇文章开始,我一直在从另一个角度看待这个问题:http://blog.joynag.net/2013/05/facades-in-laravel-4-and-static-methods-resolution/

I have been looking at this from another angle starting at this article: http://blog.joynag.net/2013/05/facades-in-laravel-4-and-static-methods-resolution/

在检查调用 File:get() 时,我最终到达了 Container 类的共享函数,它被这个实际调用参数 share(function() { return new Filesystem; }.

When examining the call File:get() I finally end up at the Container class' share function which is called with this actual parameter share(function() { return new Filesystem; }.

我无法弄清楚的是$container 的使用.特别是在闭包内第二次出现时:

What I just can't figure out is the use of $container. Especially at the second occurence within the closure:

$object = $closure($container);

你能再澄清一下吗?为什么 $container 在这里作为参数传递,其中实际包含什么?据我了解 $closure 在这一点上持有并执行 function() { return new Filesystem;} 没有输入参数.

Could you please clarify this again? Why is $container passed as a parameter here and what is actually contained in it? As far as I understand $closure at that point holds and executes function() { return new Filesystem; } which has no input parameter.

我迷路了.现在连续两天研究了这个和 PHP 匿名函数/闭包,但仍然无法弄清楚.我既不理解这里 $closure($container) 的语法和逻辑.

I am lost. Studied this and the PHP anonymous functions/closures now for two days straight and still can't figure it out. I neither understand the syntax of $closure($container) here nor the logic.

推荐答案

参考,这是share方法@v4.0.5.

For reference, this is the share method @ v4.0.5.

那么,这里发生了什么.我会分几个步骤来解释.

So, what's happening here. I'll explain it in a couple of steps.

正如您所指出的,此方法是从服务提供商处调用的.所以,FilesystemServiceProvider 调用这个方法,看起来像这样:

As you pointed out this method is called from service providers. So, the FilesystemServiceProvider calls this method which looks something like this:

$this->app['files'] = $this->app->share(function() { return new Filesystem; });

它将此 share 方法的 return 值分配给容器中的绑定.简而言之,返回值将是闭包中返回的新 Filesystem 实例.

It's assigning the return value of this share method to a binding in the container. In a nutshell, that return value will be the new Filesystem instance that is returned in the closure.

share 方法只是在 IoC 容器中定义单例的另一种方式.所有这些起初可能有点令人生畏.基本上,Laravel 本身就是一个 IoC 容器.所有类都绑定为容器上的实例.有时这些实例应该是每次调用时相同的实例.

The share method is just another way of defining a singleton in IoC container. All this can be a bit intimidating at first. Basically, Laravel itself is an IoC container. All the classes are bound as instances on the container. Sometimes these instances should be the same instance on every call.

如果您查看上面 GitHub 上的引用方法,您会注意到在闭包内部定义了一个静态变量.然后检查该变量是否为空,如果是则解析闭包(这是返回我们新的 Filesystem 实例的闭包).然后它简单地返回变量.

If you take a look at the referencing method above on GitHub, you'll notice that inside the closure a static variable is defined. It then checks if that variable is null, and if it is it resolves the closure (this is the closure that returns our new Filesystem instance). Then it simply returns the variable.

现在,下次使用 File::get() 时,不需要再次实例化 Filesystem 类,因为它已经实例化并存储在静态 $object 变量.所以它只是简单地将同一个对象返回给你.

Now, the next time you use File::get() it doesn't need to instantiate the Filesystem class again, because it's already been instantiated and stored in the static $object variable. So it simply returns the same object to you.

所以!真的,你可以用这个替换 $this->app['files'] 行,它仍然可以工作.

So! Really, you could replace the $this->app['files'] line with this, and it would still work.

$this->app->instance('files', new Filesystem);

99% 的服务实际上使用 share 方法,因为在闭包内部工作允许使用更复杂的依赖项实例化对象.

99% of services actually use the share method though because working inside a closure allows objects to be instantiated with more complex dependencies.

希望这会有所帮助.

这篇关于Laravel 4 - 容器类:共享功能 &闭合逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆