Skip to content
Advertisement

Laravel container and shared instances

I am wondering how Laravel differentiates between singletons(shared instances) and concrete implementations that might be overwritten inside the container.

The container has a bind method that looks like this:

JavaScript

It also has a singleton method that calls this function but with the $shared argument always being true like so:

JavaScript

The difference here being that although they both are bound in the $bindings property the singleton set it like so:

JavaScript

How does this make it a singleton though if there seems to be no check if it has already been set or not? Nowhere can I find whether it does anything with the $shared variable we set.

Besides that there is also another property in this class called:

JavaScript

It would seem logical for a singleton to end up here, so what exactly does this

Example of the bind method:

https://github.com/laravel/framework/blob/5.3/src/Illuminate/Container/Container.php#L178

Advertisement

Answer

bind() method saves $shared here. Then make() method is using isShared() method for checking if $shared is set and then for checking if it’s true or false here.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement