Laravel核心方法的困惑 [英] Laravel core method confusion

查看:84
本文介绍了Laravel核心方法的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Laravel的核心,因为我想了解它是如何工作的.但是我想出了一种方法,即使经过3天,我也无法将头缠住. 在start.php中,该应用程序已绑定到自身.到目前为止,一切都很好.但是当我检查$ app-> share方法时,我迷路了.

I have been digging in the core of Laravel because I would like to understand how it works. But I come up with a method that I just cannot wrap my head around even after 3 days. In start.php the app is binded to itself. So far so good. But when I check the $app->share method I am lost.

    public function share(Closure $closure)
{
    return function($container) use ($closure)
    {

        // We'll simply declare a static variable within the Closures and if
        // it has not been set we'll execute the given Closure to resolve
        // the value and return it back to the consumers of the method.
        static $object;
        if (is_null($object))
        {
            $object = $closure($container);
        }

        return $object;
    };
}

此方法返回一个匿名函数,该函数在执行时将返回应用程序的实例.我看到正确的吗?为什么是这样?为什么要返回闭包,而不仅仅是返回实例.这似乎是一种奇怪的方法,但是我很确定这是有原因的;)??

This method returns an anonymous function which when executed returns an instance of the app. Do I see that right? Why is this? Why do you want to return a closure and not just the instance. This seems like a strange way, but I am quite sure that there is a reason ;) ??

更新 start.php中的行:

UPDATE The line in start.php:

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

所以我认为$ app ['app']是一个关闭对象.但是,如果我执行get_class,则该类为Illuminate \ Foundation \ Application. 此外,由于$ app'app'显然无法正常工作,因此也无法执行它.

So I would think that $app['app'] is a closure object. However if I do get_class the class is Illuminate\Foundation\Application . Furthermore there is also no way to execute it as $app'app' will not work obviously.

推荐答案

$app不是普通数组,它实际上是Illuminate\Foundation\Application

$app is no normal array, it is actually an instance of Illuminate\Foundation\Application1, an extension of Illuminate\Container\Container2, which implements ArrayAccess. But you know this already, as that's where the share() method lives.

容器将键绑定到闭包,当访问键时,将从内存中获取值,或者在第一次访问时,调用绑定的闭包并返回结果值.在容器上设置密钥后,除非已将其封装在容器中,否则它将被包装在容器中.

The container binds keys to closures, when the keys are accessed the value is fetched from memory or, on first access, the bound closure is called and the resulting value is returned. When a key is set on the container it is wrapped in a closure unless it is already a closure.

这为容器提供了一个一致的内部接口,因此该代码不会不断地类型检查其内容.它还只会将您实际使用的引用加载到内存中-认为闭包的占用空间比完全加载的类实例的占用空间轻.但是一旦加载,您就可以为其余的请求使用相同的实例.

This provides a consistent internal interface for the container, so that the code is not constantly type checking its contents. It will also only load the references you actually use are into memory - it is thought that the footprint of a closure is lighter than that of a fully loaded class instance. But once loaded, you get the benefit of working with the same instance for the rest of the request.

为什么我不知道为什么不使用instance()在容器上注册应用-也许它在跟踪和转储输出中生成了递归引用.

Why the app is not registered on the container using instance() I don't know though - perhaps it produces recursive references in trace and dump output.

这篇关于Laravel核心方法的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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