use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; $container = new ContainerBuilder(); $definition = new Definition('MyClass'); $container->setDefinition('my_service', $definition);
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; $container = new ContainerBuilder(); $container->register('database_connection', 'DatabaseConnection'); $container->register('my_service', 'MyService') ->addArgument(new Reference('database_connection'));
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; $container = new ContainerBuilder(); $container->register('my_service', 'MyService') ->addArgument(new Reference('logger')) ->addMethodCall('setDatabaseConnection', array(new Reference('database_connection'))) ->addTag('my_service_tag');In this example, a service "my_service" is registered with a method call to setDatabaseConnection and a tag "my_service_tag". Overall, the ContainerBuilder provides a powerful and flexible way to manage dependencies and services in Symfony applications.