Symfony

Dependency Injection & Services

Service container, autowiring, service configuration, tags, decoration, aliases

24 interview questionsยท
Mid-Level
1

What is the main role of the service container in Symfony?

Answer

The service container (or Dependency Injection Container) is responsible for creating, configuring, and managing the lifecycle of all application services. It enables automatic dependency injection into classes, promoting loose coupling and facilitating unit testing.

2

What is autowiring in Symfony?

Answer

Autowiring is a feature that allows the container to automatically resolve service dependencies by analyzing the type-hints of constructor parameters. This avoids having to manually configure each dependency injection in YAML or PHP files.

3

What is the difference between a public and a private service in Symfony?

Answer

A public service can be retrieved directly from the container via get(), while a private service can only be injected as a dependency of other services. Since Symfony 4, services are private by default to improve performance and encourage dependency injection over direct container access.

4

How to inject a service into a Symfony controller using autowiring?

5

What does the autoconfigure option do in Symfony service configuration?

+21 interview questions

Master Symfony for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free