0Pricing
PHP Academy · Lesson

DependencyInjection Container

Register services and wire dependencies with the Symfony DI container.

DependencyInjection Container is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is Dependency Injection?

Dependency Injection (DI) is a pattern where a class receives its dependencies from the outside rather than creating them internally — making code testable and loosely coupled.

The Symfony DI Component

Symfony's symfony/dependency-injection component provides a powerful IoC container that can auto-wire, configure, and cache service definitions.

$ composer require symfony/dependency-injection

Creating a Container

Build a container and register services.

<?php
use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();

// Register a service:
$container->register("logger", \App\Logger::class);

Adding Constructor Arguments

Pass constructor arguments to service definitions.

<?php
$container->register("mailer", \App\Mailer::class)
    ->addArgument("smtp.example.com")
    ->addArgument(587);

Service References

Inject one service into another using a Reference.

<?php
use Symfony\Component\DependencyInjection\Reference;

$container->register("email_service", \App\EmailService::class)
    ->addArgument(new Reference("mailer"))
    ->addArgument(new Reference("logger"));

Retrieving a Service

Fetch a service from the container. Dependencies are resolved automatically.

<?php
$emailService = $container->get("email_service");
$emailService->send("user@example.com", "Welcome!");

Autowiring

With autowiring enabled, the container inspects type hints and injects matching services automatically.

<?php
$container->register(EmailService::class)
    ->setAutowired(true);
// Constructor is: public function __construct(Mailer $mailer, Logger $logger)

Parameters

Store configuration values as container parameters.

<?php
$container->setParameter("mailer.host", "smtp.example.com");
$container->setParameter("mailer.port", 587);

// Reference in service definition:
$container->register("mailer", Mailer::class)
    ->addArgument("%mailer.host%")
    ->addArgument("%mailer.port%");

YAML Configuration

Define services in YAML (requires symfony/config and symfony/yaml).

# services.yaml
services:
    App\Mailer:
        arguments:
            - "%mailer.host%"
            - "%mailer.port%"

Compiling the Container

Compile the container for performance. This validates definitions and optimises the service graph.

<?php
$container->compile();
// Dump to PHP cache file:
$dumper = new PhpDumper($container);
file_put_contents("CachedContainer.php", $dumper->dump());

Shared vs Non-Shared Services

By default services are shared (singleton). Set shared(false) to get a new instance each time the service is retrieved.

Summary

The Symfony DI container registers, wires, and resolves services. Use autowiring for convenience, parameters for config, and compile the container in production for performance.

Quick Check

What does autowiring do in the Symfony DI container?

Frequently asked questions

Is the “DependencyInjection Container” lesson free?

Yes — the full text of “DependencyInjection Container” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “DependencyInjection Container”?

Register services and wire dependencies with the Symfony DI container. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “DependencyInjection Container” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. HttpFoundation: Request and Response
  2. DependencyInjection Container
  3. EventDispatcher: Decoupled Events
  4. Console Component: CLI Commands
← Back to PHP Academy