Laravel:差异App :: bind和App :: singleton [英] Laravel: Difference App::bind and App::singleton

查看:588
本文介绍了Laravel:差异App :: bind和App :: singleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于laravel在IOC容器和外墙方面提供的所有精美功能,我有些困惑.由于我不是一个经验丰富的程序员,所以学习变得不知所措.

I get a bit confused over all the nice things laravel has to offer in terms of the IOC container and facades. Since I'm not an experienced programmer it gets overwhelming to learn.

我想知道这两个示例之间有什么区别

I was wondering, what is the difference between these two examples:

  1. "Foo"的外观,并通过App::bind()

"Foo"的外观,并通过App::singleton()

A facade to 'Foo' and registered in the container via App::singleton()

以我的最佳理解,Foo::method()将被重写为$app->make['foo']->method(),因此在第一个示例中,将创建Foo类的多个实例,在第二个示例中,由于它是通过App::singleton()绑定的,因此相同每次调用该对象上的Method时,都将返回Foo的实例.

In my best understanding Foo::method() will be rewritten as $app->make['foo']->method() so in the first example multiple instances of the Foo class will be created and in the second example, since it's bound via an App::singleton(), the same instance of Foo will be returned every time a Method on that object is called.

很抱歉,这个问题的答案很明显,但是我找不到关于此事的任何确认,而且在任何地方都没有明确的解释.

I'm sorry if the answer to this question is to obvious, but I can't find any confirmation on this matter and nowhere this is clearly explained.

推荐答案

就是这样.

一个非常简单的证明就是测试行为.由于Laravel应用程序只是扩展了Illuminate\Container\Container,我们将仅使用容器(在我的情况下,我甚至仅将容器作为对我的composer.json的依赖项添加到容器中)进行测试.

A very simple proof is to test out the bevahior. Since the Laravel Application simply extends Illuminate\Container\Container, we'll use just the container (in my case I even only added the container as dependency to my composer.json) to test.

require __DIR__ . '/vendor/autoload.php';

class FirstClass
{
    public $value;
}

class SecondClass
{
    public $value;
}

// Test bind()
$container = new Illuminate\Container\Container();

$container->bind('FirstClass');

$instance = $container->make('FirstClass');
$instance->value = 'test';

$instance2 = $container->make('FirstClass');
$instance2->value = 'test2';

echo "Bind: $instance->value vs. $instance2->value\n";

// Test singleton()
$container->singleton('SecondClass');

$instance = $container->make('SecondClass');
$instance->value = 'test';

$instance2 = $container->make('SecondClass');
$instance2->value = 'test2'; // <--- also changes $instance->value

echo "Singleton: $instance->value vs. $instance2->value\n";

结果符合预期:

Bind: test vs. test2

Singleton: test2 vs. test2

可能是肮脏的证据,但确实是一个证据.

Might be a dirty proof, but indeed it is one.

所有魔力都在于Container::make方法. 如果绑定注册为共享(即单身),则返回类实例,否则每次返回一个新实例.

All the magic lies in the Container::make method. If the binding is registered as shared (which means as singleton), the class instance is returned, otherwise a new instance every time.

来源: https://github.com/laravel/framework /blob/4.2/src/Illuminate/Container/Container.php#L442

BTW,Container::singletonContainer::bind相同,第三个参数设置为true.

BTW, Container::singleton is the same as Container::bind with the third parameter set to true.

这篇关于Laravel:差异App :: bind和App :: singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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