🐘 PHP Intermediate

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

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

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.

Common Mistake

A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.