Laravel 4 - 集装箱类:分享功能&闭合逻辑 [英] Laravel 4 - Container class: share function & closure logic

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

问题描述

Hello stackoverflow社区,

Hello stackoverflow community,

我有一个后续问题dsicussed这里:
Laravel核心方法混淆

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

(上面的quesion的作者)以前,目前习惯了Laravel 4 FW和检查核心。虽然给出了一个确切的答案,我仍然不明白逻辑和发生在底下发生了什么。所以我非常感谢进一步的解释。
我知道这可能是一个重复,但由于我不能发表评论,但我会给它一个新的问题。

I am in the same situation as driechel (author of quesion 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()结束于使用此实际参数 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. So any help is appreciated.

非常感谢。

推荐答案

作为参考,这是 共享 method @ v4.0.5

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

这里发生了什么。

正如你指出的那样方法从服务提供者调用。所以, 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; });

正在分配此共享的 return 方法绑定到容器中的绑定。简而言之,该返回值将是在闭包中返回的新的 Filesystem 实例。

共享方法只是在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,注意在闭包内部定义了一个静态变量。然后它检查该变量是否为null,如果是,则解析闭包(这是返回我们新的 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天全站免登陆