I am wondering how Laravel distinguishes between singles (shared instances) and specific implementations that can be overwritten inside a container.
There is a binding method in the container that looks like this:
public function bind($abstract, $concrete = null, $shared = false)
{
$this->dropStaleInstances($abstract);
if (is_null($concrete)) {
$concrete = $abstract;
}
if (!$concrete instanceof Closure) {
$concrete = $this->getClosure($abstract, $concrete);
}
$this->bindings[$abstract] = compact('concrete', 'shared');
if ($this->resolved($abstract)) {
$this->rebound($abstract);
}
}
It also has a singleton method that calls this function, but with a common argument, $ is always true:
public function singleton($abstract, $concrete = null)
{
$this->bind($abstract, $concrete, true);
}
The difference is that although both are related in the property $bindings, singleton sets it like this:
[concrete, true]
How does this make it solitary, although if there seems to be no check if it is already installed or not? Nowhere can I find if he is doing anything with the $ shared variable we set.
In addition, there is another property in this class:
protected $instances = [];
It would seem logical that the singleton will be here, so what exactly is it
:
https://github.com/laravel/framework/blob/5.3/src/Illuminate/Container/Container.php#L178