🐘 PHP Intermediate

What is a Service Container (IoC Container) in PHP?

Answer

A Service Container (also called an IoC Container or DI Container) is an object that manages class instantiation and dependency resolution automatically. You register services (bindings) into the container: $container->bind(LoggerInterface::class, FileLogger::class). When you request a class, the container resolves all its constructor dependencies recursively using Reflection. Laravel's container (app()) is one of the most powerful — it supports automatic injection, contextual bindings, singletons (singleton()), and method injection. The container makes dependency injection practical at scale — instead of manually wiring hundreds of classes, the container does it automatically. This is the core of how modern PHP frameworks work internally.