1

Edit: rephrased the question

If you call a method on a facade class, will an actual object of that class be instantiated for calling that method, or will an object of that class be instantiated during bootstrapping the application and that same object will be returned every time (like a singleton)?

I understand that normal to the container bound classes will be instantiated when

App::make('class name')
is done, but since you don't have to manually use App::make I don't understand where the object or instance of the class "lives"?

BTW I understand that facades in laravel aren't the same thing as the design pattern.

Luuk Van Dongen
  • 2,391
  • 6
  • 26
  • 40

1 Answers1

1

I think Dayle Rees explains it best:

Each Facade is linked to an instance of a component in the container. The static methods of a Facade are shortcuts, and when called, they call the appropriate public method of the object that they represent within the container

So, when a method like

Cache::get('key');

is called, it is actually resolved in the IoC container to

$app->make('cache')->get('key');

Now that instance of Cache is within $app, or the main Container.

Edit: All classes are registered, not necessarily instantiated during the bootstrapping of Laravel.

Brobin
  • 3,241
  • 2
  • 19
  • 35
  • Thanks! Now on subsequent calls of that facades, will a new instance of the object be created again, or is it so that object now exists in the container and will be returned for all other method calls? – Luuk Van Dongen Aug 15 '14 at 18:46
  • Since each Facade is linked to an instance of the underlying class in the container, I believe it would only be instatiated once. [Here is Dayle Rees' explanation of the architecture in Codebright](http://daylerees.com/codebright/architecture) – Brobin Aug 15 '14 at 18:49
  • So putting it that way, it won't make a difference for binding the underlying class (the one the facade calls upon) via App::bind or App::singleton since one and the same instance will be returned anyway? – Luuk Van Dongen Aug 15 '14 at 18:51
  • I stand corrected. I just took a deeper look into the docs and when `App::make()` is called, it will return a new instance of the bound object. `App::singleton()` is the case where the container can only ever have one instance of a class. `App::bind()` is used in Laravel's bootstrap to bind classes to the container. [link in the docs](http://laravel.com/docs/ioc#basic-usage) – Brobin Aug 15 '14 at 18:58
  • But this makes a difference in relation to Facades? In other words underlying classes of facades will be instantiated multiple times if the class is bound via App::bind('ClassName') instead of App::singleton? I'm asking this because I've encountered another problem some time ago: http://stackoverflow.com/questions/25307870/laravel-do-facades-actually-create-new-objects-on-calling-methods – Luuk Van Dongen Aug 15 '14 at 23:41
  • Correct. As far as I understand, if you use `App::bind('foo')`, you can end up with multiple instances of foo. If you use `App::singleton('foo')` you will only ever end up with one instance of foo. – Brobin Aug 15 '14 at 23:50
  • But the problem I found in the stackoverflow link I posted says otherwise... I'm really confused :( – Luuk Van Dongen Aug 16 '14 at 00:11