IoC — Inversion of Control: the principle behind every container
Definition
Inversion of Control (IoC) is a design principle in which a class does not create or locate its own dependencies — instead, control over dependency creation is transferred to an external orchestrator. The class declares what it needs, and something else (the framework, a container, or a test harness) decides what to provide. IoC is a principle, not a mechanism; several patterns (Dependency Injection, Service Locator, event systems) are all implementations of it.
In Practice
<?php
// WITHOUT IoC — the class controls its own dependencies
class OrderService
{
private Mailer $mailer;
public function __construct()
{
// Hard-coded, impossible to swap in tests
$this->mailer = new SmtpMailer('smtp.example.com', 587);
}
}
// WITH IoC — the orchestrator controls dependency creation
class OrderService
{
public function __construct(
private readonly Mailer $mailer, // declared, not created
) {}
}
// Laravel's container is the orchestrator:
// app()->bind(Mailer::class, SmtpMailer::class);
// $service = app(OrderService::class); // container injects SmtpMailerIn a Laravel codebase you see IoC everywhere: controllers receive services via constructor arguments they never instantiate, commands receive repositories they never build, and listeners receive loggers they never configure. The container is the thing that inverted the control.
In Context
In interviews, IoC is often confused with Dependency Injection. The correct framing: IoC is the broad principle ("stop calling us, we'll call you"), and DI is one specific technique to achieve it. You can have IoC without DI — an event system inverts control by having the framework call your listener rather than you calling the framework.
A common misconception is that IoC is a framework feature. It is a programming principle that exists in any language. PHP developers encounter it most concretely through Laravel's service container, but the principle predates Laravel by decades and appears in Java's Spring, Python's dependency injectors, and manual object graphs wired in plain PHP.
Related terms: Dependency Injection (the constructor-argument form of IoC), Service Container (the runtime mechanism that implements IoC in Laravel), Service Locator (another IoC implementation where classes ask a registry for dependencies — considered an anti-pattern when overused). The Hollywood Principle ("don't call us, we'll call you") is the informal name for IoC in framework design.