diff --git a/service_container/factories.rst b/service_container/factories.rst index 36a9a2e7db8..6db7e7261bf 100644 --- a/service_container/factories.rst +++ b/service_container/factories.rst @@ -100,6 +100,79 @@ create its object: the configured class name may be used by compiler passes and therefore should be set to a sensible value. +Using the Class as Factory Itself +--------------------------------- + +When the static factory method is on the same class as the created instance, +the class name can be omitted from the factory declaration. +Let's suppose the ``NewsletterManager`` class has a ``create()`` method that needs +to be called to create the object and needs a sender:: + + // src/Email/NewsletterManager.php + namespace App\Email; + + // ... + + class NewsletterManager + { + private string $sender; + + public static function create(string $sender): self + { + $newsletterManager = new self(); + $newsletterManager->sender = $sender; + // ... + + return $newsletterManager; + } + } + +You can omit the class on the factory declaration: + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + # ... + + App\Email\NewsletterManager: + factory: [null, 'create'] + arguments: + $sender: 'fabien@symfony.com' + + .. code-block:: xml + + + + + + + + + + + + + .. code-block:: php + + // config/services.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; + + use App\Email\NewsletterManager; + + return function(ContainerConfigurator $containerConfigurator) { + $services = $containerConfigurator->services(); + + // Note that we are not using service() + $services->set(NewsletterManager::class) + ->factory([null, 'create']); + }; + Non-Static Factories --------------------