$definition = new Definition('My\Service'); $definition->setFactoryClass('My\Factory\ServiceFactory'); // $definition->setArguments(...); $container->setDefinition('my_service', $definition);
class MyService { public function __construct(My\Dependency\Dependency $dependency) {} } $definition = new Definition('MyService'); $definition->setFactoryClass('My\Service\ServiceFactory'); $definition->addArgument(new Reference('my_dependency')); $container->setDefinition('my_service', $definition);In this example, we define a service 'MyService' which depends on a 'My\Dependency\Dependency' object. We set the factory class as 'My\Service\ServiceFactory' and pass a Reference to the 'my_dependency' service as an argument to the factory method. This ensures that the dependency is injected into the service instance created by the factory. Package library: Symfony\Bundle\FrameworkBundle\DependencyInjection.