use Symfony\Component\DependencyInjection\ContainerInterface; class UserService { private $dbConnection; public function __construct(ContainerInterface $container) { $this->dbConnection = $container->get('database_connection'); } public function getUsers() { // logic to retrieve users from database } }
use Symfony\Component\DependencyInjection\ContainerInterface; class UserController { private $router; public function __construct(ContainerInterface $container) { $this->router = $container->get('router'); } public function showUser($id) { // logic to retrieve user with id from database $url = $this->router->generate('user_view', ['id' => $id]); return $this->redirectToRoute($url); } }In both examples, we use the get method of the ContainerInterface to retrieve a specific dependency from the container, rather than hardcoding it into the class.