Service container is used to resolve an instance of a class thereby making dependency injection possible. It’s a way to abstract your app services so they can be easily swapped out for a different implementation. First you bind it to the service container:
App::bind('MyService', MyService1::class);
So, by binding a class into the service container, we can do things like:
public function __construct(MyService $service){}
But now what if we want to change MyService to a different implementation? Without the service container we would have to change
MyService $service
everywhere it is used.
But with the service container, we can simply change the binding:
App::bind('MyService', MyService2::class);
Without having to change:
public function __construct(MyService $service){}
Furthermore, if MyService1 and MyService2 implement an interface, then the code that depends on MyService1 should function perfectly after changing the dependency to MyService2.
I guess an example would be file storage. Let’s say we have a service for storing files in Amazon’s S3 cloud, but later we want to change to Rackspace. By using the service container, the change would be much easier.
The idea behind dependency injection is code maintainability. If your project is so small that maintainability doesn’t matter, then don’t worry about it.