Laravel - difference between singleton and instance binding in service container
Asked Answered
A

2

6

In the service container of Laravel, I can bind both singleton and instance. From the Laravel doc:

Binding A Singleton

The singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:

$this->app->singleton('HelpSpot\API', function ($app) {
    return new HelpSpot\API($app->make('HttpClient'));
});

Binding Instances

You may also bind an existing object instance into the container using the instance method. The given instance will always be returned on subsequent calls into the container:

$api = new HelpSpot\API(new HttpClient);

$this->app->instance('HelpSpot\API', $api);

Q1) So what is the difference between the two concepts? Can I guess that for singleton binding, Laravel upon first request builds the object itself through internal service container mechanism and then supplies it on subsequent calls whereas in the case of instance binding, service container is explicitly given an already built object which it supplies on every request?

Or is there any other explanation?

Q2) Why do we need both binding options?

Adjudge answered 7/1, 2020 at 20:55 Comment(0)
C
8

Difference between singleton and instance

The two concepts are very much alike. The only difference is, indeed, the fact that you either pass in a class/interface or an object.

Singleton docs:

The singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container

Instance docs

You may also bind an existing object instance into the container using the instance method. The given instance will always be returned on subsequent calls into the container

Why do we need both?

The answer to this question is probably rooted in the Laravel philosophy. From what I see in most of the features Laravel provides, there's more than one way to solve an issue. It feels like this is one of those as well. There's slight differences which might make singleton or instance usage preferable in some cases.

Singleton usage

The use of singleton will help keep your application light, as these classes/interfaces do not get created if they are not used.

Instance usage

In some cases you might've already created an object, which you still need to inject into other places. That's where instances comes in.

Concerning answered 7/1, 2020 at 22:43 Comment(2)
'In some cases you might've already created an object, which you still need to inject into other places' - if I had created the object elsewhere, how would I access that from within the service container ? Can you give an example ?Adjudge
@IstiaqueAhmed lets assume you are creating some functionality and you want to create some object, accessible everywhere, but based or actual situation. Eg: user has been locked, you created an LockedUser record. But will be extremely nice to use this record while building notification view or so. Binding an instance will let you do something like this: ``` $record = LockedUser::create([/* some data here */]); app()->instance('locked_record', $record); // later in code $model = app('locked_record'); // hooray! $record from above returned! ```Laky
E
2

Additional to what @PtrTon said, the difference is also in the time, the instance is created. Using an instance, the instance is—of course— created before passing to the service container, meaning it's created quite early. Whereas using a singleton, the instance is created the first time, the binding is resolved as far as I know which can be a considerable amount of time after the example where you pass an instance.

Empoverish answered 2/8, 2021 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.