14

The Laravel docs indicate that the appropriate way to bind a singleton is with the App::singleton() method, but internally Laravel will use the bindShared() method (for example, in TranslationServiceProvider).

I assume that the documented approach is preferred, but is there a functional difference? If not, is there any reason for having two approaches (beyond maybe historical accident)?

2
  • That forum post is about the share() method, not the bindShared() method. The question seems to be fundamentally the same, but the answer there doesn't really address what I raise above. I know that the two methods are approximately the same; I'm looking to find out what the difference in nuance is and when/why one should be preferred. May 29, 2014 at 14:43

4 Answers 4

8

I've been wondering the same thing. I don't know the motivations behind this, but I can speak to a few differences.

Here is the definition of the two methods from Laravel 4.2:

public function singleton($abstract, $concrete = null)
{
    $this->bind($abstract, $concrete, true);
}

public function bindShared($abstract, Closure $closure)
{ 
    $this->bind($abstract, $this->share($closure), true);
}

Similarities:

  • Both methods call bind() under the hood.
  • Both methods pass true to the 3rd parameter of bind(), which signifies that this is a shared object.
  • In both cases, because this is a shared object, a call to isShared($abstract) will return true.
  • In both cases, because this is a shared object, a call to make($abstract) will return only the first instance.

Differences:

  • singleton() will accept a Closure or a string. bindShared() will only accept a Closure, not a string.
  • bindShared(), in addition to binding the object into the IOC container as a shared object, takes the additional step of wrapping the passed Closure in a share'd Closure, which prevents the passed Closure from being executed more than once. At first glance, this appears to be a double assurance that the object will be treated as a singleton. I can only guess why this might be desirable.
  • bindShared() is called 87 times inside the framework. singleton() is called 0 times.
1
  • 2
    Looks like all bindShared() calls have been changed to singleton() in Laravel 5.0 framework. The implementations of the two methods seems to be the same. Mar 2, 2015 at 20:15
7

They are (were) functionally identical, except that bindShared() only accepts closures.

Thus bindShared() has been deprecated in Laravel 5.1 (PR 9009 - commit 829060f) and removed in Laravel 5.2 (PR 9037).

Case finally solved :)

2

bind($abstract, $concrete, $shared) Adds $abstract as a key to the container, with $concrete being the concrete class to instantiate in its place. Mainly used for providing a concrete implementation for an interface.

share($closure) Given a closure (only), makes it act as if it was shared (instance/singleton style), and returns it. Technically equivalent to App::bind($key, $closure, true) but goes about it a different way. Mainly used in service providers to add a fully resolvable service to the IoC container.

bindShared($abstract, $closure) A shortcut that was introduced in 4.1 that caters to a common pattern. Essentially helps those who want to bind a shared instance in the container. See below for example.

singleton($abstract, $concrete) Simply an alias to bind with the $shared argument set to true. Mainly used for providing a concrete implementation for an interface, but one that should only have one instance (database connection, etc.)."

This is from http://alexrussell.me.uk/laravel-cheat-sheet/ I think this link should be helpfull

1
  • 3
    This explains indirectly that singleton() and bindShared() are the same, but it doesn't really answer the question of why there are two approaches. The article just says they "go about it in a different way." Well, yeah. But why? :-) Jul 13, 2014 at 22:09
0

I think mainly for backwards compatibility. Singleton explains better behavior than bindShared. Removing bindShared would mean Package developers to refactor their code.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.