Motive 1
Some packages need to.
Usually you are going to see ServiceProviders using it this way:
$this->app['db']
Or inside a closure binding:
$this->app->bindShared('anything', function($app)
{
return new DatabaseClass($app['db']->connection());
});
Because $app
is a property of Illuminate\Support\ServiceProvider
and ServiceProviders are the guys who boot up services that will be used by Facades in your app.
So, first Laravel will instantiate and boot all ServiceProviders who provides the IoC binded service, for instance:
$this->app->bindShared('db', function($app)
{
return new DatabaseManager($app, $app['db.factory']);
});
And after that particular call you have access to the Facade:
DB::table(...);
Before that, what you get is an error telling you that there is no 'db' binded.
And I have to stress that you will mostly see that being used on ServiceProviders that way, because the closure provides the $app
variable to use it this way:
$app['db']->connection()...
Of course anyone can get the IoC binding for app and set it to an $app
var...
Motive 2
Some others might overuse that to not break their packages easily. Relying on an Alias to make your package work is dangerous, because if the user changes that alias the package could break. That might be a user problem, but it would create some problems to them too, like issues on Github.