$container = new ContainerBuilder(); $container->setParameter('database_host', 'localhost'); $container->setParameter('database_user', 'user'); $container->setParameter('database_password', 'password'); $container->setParameter('mailer_transport', 'smtp'); $container->setParameter('mailer_host', 'localhost'); $container->setParameter('mailer_user', 'user'); $container->setParameter('mailer_password', 'password'); $container->register('mailer', 'Mailer') ->addArgument('%mailer_transport%') ->addArgument('%mailer_host%') ->addArgument('%mailer_user%') ->addArgument('%mailer_password%'); $mailer = $container->get('mailer');
use Symfony\Component\DependencyInjection\ContainerInterface; class Foo { private $container; public function __construct(ContainerInterface $container) { $this->container = $container; } public function doSomething() { $logger = $this->container->get('logger'); $logger->info('I did something.'); } }This example shows how to use the set method in a class called "Foo". The class constructor takes the ContainerInterface as a parameter, which allows for the "logger" service to be retrieved through the get method. The logger service is used to output information when the "doSomething" method is called. Overall, the Symfony\Component\DependencyInjection\ContainerInterface package library provides developers with an organized and efficient way to manage dependencies within a PHP application.