Skip to content

Using a Factory to Create Services

Warning: You are browsing the documentation for Symfony 5.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.2 (the current stable version).

Symfony's Service Container provides multiple features to control the creation of objects, allowing you to specify arguments passed to the constructor as well as calling methods and setting parameters.

However, sometimes you need to apply the factory design pattern to delegate the object creation to some special object called "the factory". In those cases, the service container can call a method on your factory to create the object rather than directly instantiating the class.

Static Factories

Suppose you have a factory that configures and returns a new NewsletterManager object by calling the static createNewsletterManager() method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// src/Email/NewsletterManagerStaticFactory.php
namespace App\Email;

// ...

class NewsletterManagerStaticFactory
{
    public static function createNewsletterManager(): NewsletterManager
    {
        $newsletterManager = new NewsletterManager();

        // ...

        return $newsletterManager;
    }
}

To make the NewsletterManager object available as a service, use the factory option to define which method of which class must be called to create its object:

1
2
3
4
5
6
7
# config/services.yaml
services:
    # ...

    App\Email\NewsletterManager:
        # the first argument is the class and the second argument is the static method
        factory: ['App\Email\NewsletterManagerStaticFactory', 'createNewsletterManager']

Note

When using a factory to create services, the value chosen for class has no effect on the resulting service. The actual class name only depends on the object that is returned by the factory. However, 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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:

1
2
3
4
5
6
7
8
# config/services.yaml
services:
    # ...

    App\Email\NewsletterManager:
        factory: [null, 'create']
        arguments:
            $sender: 'fabien@symfony.com'

Non-Static Factories

If your factory is using a regular method instead of a static one to configure and create the service, instantiate the factory itself as a service too. Configuration of the service container then looks like this:

1
2
3
4
5
6
7
8
9
10
11
# config/services.yaml
services:
    # ...

    # first, create a service for the factory
    App\Email\NewsletterManagerFactory: ~

    # second, use the factory service as the first argument of the 'factory'
    # option and the factory method as the second argument
    App\Email\NewsletterManager:
        factory: ['@App\Email\NewsletterManagerFactory', 'createNewsletterManager']

Invokable Factories

Suppose you now change your factory method to __invoke() so that your factory service can be used as a callback:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// src/Email/InvokableNewsletterManagerFactory.php
namespace App\Email;

// ...
class InvokableNewsletterManagerFactory
{
    public function __invoke(): NewsletterManager
    {
        $newsletterManager = new NewsletterManager();

        // ...

        return $newsletterManager;
    }
}

Services can be created and configured via invokable factories by omitting the method name:

1
2
3
4
5
6
7
# config/services.yaml
services:
    # ...

    App\Email\NewsletterManager:
        class:   App\Email\NewsletterManager
        factory: '@App\Email\InvokableNewsletterManagerFactory'

Passing Arguments to the Factory Method

Tip

Arguments to your factory method are autowired if that's enabled for your service.

If you need to pass arguments to the factory method you can use the arguments option. For example, suppose the createNewsletterManager() method in the previous examples takes the templating service as an argument:

1
2
3
4
5
6
7
# config/services.yaml
services:
    # ...

    App\Email\NewsletterManager:
        factory:   ['@App\Email\NewsletterManagerFactory', createNewsletterManager]
        arguments: ['@templating']
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version