Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Components
  4. Dependency Injection
  5. Using a Factory to Create Services
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia
  • Passing Arguments to the Factory Method

Using a Factory to Create Services

Edit this page

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

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

Using a Factory to Create Services

Symfony's Service Container provides a powerful way of controlling the creation of objects, allowing you to specify arguments passed to the constructor as well as calling methods and setting parameters. Sometimes, however, this will not provide you with everything you need to construct your objects. For this situation, you can use a factory to create the object and tell the service container to call a method on the factory rather than directly instantiating the class.

Suppose you have a factory that configures and returns a new NewsletterManager object:

1
2
3
4
5
6
7
8
9
10
11
class NewsletterManagerFactory
{
    public static function createNewsletterManager()
    {
        $newsletterManager = new NewsletterManager();

        // ...

        return $newsletterManager;
    }
}

To make the NewsletterManager object available as a service, you can configure the service container to use the NewsletterManagerFactory factory class:

  • YAML
  • XML
  • PHP
1
2
3
4
5
services:
    newsletter_manager:
        class:          NewsletterManager
        factory_class:  NewsletterManagerFactory
        factory_method: createNewsletterManager
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service
            id="newsletter_manager"
            class="NewsletterManager"
            factory-class="NewsletterManagerFactory"
            factory-method="createNewsletterManager" />
    </services>
</services>
1
2
3
4
5
6
7
8
use Symfony\Component\DependencyInjection\Definition;

// ...
$definition = new Definition('NewsletterManager');
$definition->setFactoryClass('NewsletterManagerFactory');
$definition->setFactoryMethod('createNewsletterManager');

$container->setDefinition('newsletter_manager', $definition);

When you specify the class to use for the factory (via factory_class) the method will be called statically. If the factory itself should be instantiated and the resulting object's method called, configure the factory itself as a service. In this case, the method (e.g. createNewsletterManager) should be changed to be non-static:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
services:
    newsletter_manager_factory:
        class:            NewsletterManagerFactory
    newsletter_manager:
        class:            NewsletterManager
        factory_service:  newsletter_manager_factory
        factory_method:   createNewsletterManager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="newsletter_manager_factory" class="NewsletterManagerFactory" />

        <service
            id="newsletter_manager"
            class="NewsletterManager"
            factory-service="newsletter_manager_factory"
            factory-method="createNewsletterManager" />
    </services>
</container>
1
2
3
4
5
6
7
8
9
10
11
12
use Symfony\Component\DependencyInjection\Definition;

$container->setDefinition('newsletter_manager_factory', new Definition(
    'NewsletterManager'
));
$container->setDefinition('newsletter_manager', new Definition(
    'NewsletterManagerFactory'
))->setFactoryService(
    'newsletter_manager_factory'
)->setFactoryMethod(
    'createNewsletterManager'
);

Note

The factory service is specified by its id name and not a reference to the service itself. So, you do not need to use the @ syntax for this in YAML configurations.

Passing Arguments to the Factory Method

If you need to pass arguments to the factory method, you can use the arguments options inside the service container. For example, suppose the createNewsletterManager method in the previous example takes the templating service as an argument:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
services:
    newsletter_manager_factory:
        class:            NewsletterManagerFactory
    newsletter_manager:
        class:            NewsletterManager
        factory_service:  newsletter_manager_factory
        factory_method:   createNewsletterManager
        arguments:
            - "@templating"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="newsletter_manager_factory" class="NewsletterManagerFactory" />

        <service
            id="newsletter_manager"
            class="NewsletterManager"
            factory-service="newsletter_manager_factory"
            factory-method="createNewsletterManager">

            <argument type="service" id="templating" />
        </service>
    </services>
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use Symfony\Component\DependencyInjection\Definition;

// ...
$container->setDefinition('newsletter_manager_factory', new Definition(
    'NewsletterManagerFactory'
));
$container->setDefinition('newsletter_manager', new Definition(
    'NewsletterManager',
    array(new Reference('templating'))
))->setFactoryService(
    'newsletter_manager_factory'
)->setFactoryMethod(
    'createNewsletterManager'
);
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Take the exam at home

Take the exam at home

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Alain Flaus, a Symfony contributor

Thanks Alain Flaus (@halundra) for being a Symfony contributor

1 commit • 44 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia