Laravel bind vs singleton
01 Oct 2018Container::singleton
is the same as Container::bind
with the third parameter set to true
. 1
class MyClass
{
public $value;
}
public function test_bind()
{
$container = new Container();
$container->bind(MyClass::class);
$instance1 = $container->make(MyClass::class);
$instance2 = $container->make(MyClass::class);
static::assertNotSame($instance1, $instance2);
}
public function test_singleton()
{
$container = new Container();
$container->singleton(MyClass::class);
$instance1 = $container->make(MyClass::class);
$instance2 = $container->make(MyClass::class);
static::assertSame($instance1, $instance2);
}