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.1, 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

Symfony2'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 object.

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 NewsletterFactory
{
    public function get()
    {
        $newsletterManager = new NewsletterManager();
        
        // ...
        
        return $newsletterManager;
    }
}

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

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
parameters:
    # ...
    newsletter_manager.class: NewsletterManager
    newsletter_factory.class: NewsletterFactory
services:
    newsletter_manager:
        class:          "%newsletter_manager.class%"
        factory_class:  "%newsletter_factory.class%"
        factory_method: get
1
2
3
4
5
6
7
8
9
10
11
12
13
<parameters>
    <!-- ... -->
    <parameter key="newsletter_manager.class">NewsletterManager</parameter>
    <parameter key="newsletter_factory.class">NewsletterFactory</parameter>
</parameters>

<services>
    <service id="newsletter_manager" 
             class="%newsletter_manager.class%"
             factory-class="%newsletter_factory.class%"
             factory-method="get"
    />
</services>
1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\DependencyInjection\Definition;

// ...
$container->setParameter('newsletter_manager.class', 'NewsletterManager');
$container->setParameter('newsletter_factory.class', 'NewsletterFactory');

$container->setDefinition('newsletter_manager', new Definition(
    '%newsletter_manager.class%'
))->setFactoryClass(
    '%newsletter_factory.class%'
)->setFactoryMethod(
    'get'
);

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 (as in this example), configure the factory itself as a service:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
parameters:
    # ...
    newsletter_manager.class: NewsletterManager
    newsletter_factory.class: NewsletterFactory
services:
    newsletter_factory:
        class:            "%newsletter_factory.class%"
    newsletter_manager:
        class:            "%newsletter_manager.class%"
        factory_service:  newsletter_factory
        factory_method:   get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<parameters>
    <!-- ... -->
    <parameter key="newsletter_manager.class">NewsletterManager</parameter>
    <parameter key="newsletter_factory.class">NewsletterFactory</parameter>
</parameters>

<services>
    <service id="newsletter_factory" class="%newsletter_factory.class%"/>
    <service id="newsletter_manager" 
             class="%newsletter_manager.class%"
             factory-service="newsletter_factory"
             factory-method="get"
    />
</services>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use Symfony\Component\DependencyInjection\Definition;

// ...
$container->setParameter('newsletter_manager.class', 'NewsletterManager');
$container->setParameter('newsletter_factory.class', 'NewsletterFactory');

$container->setDefinition('newsletter_factory', new Definition(
    '%newsletter_factory.class%'
))
$container->setDefinition('newsletter_manager', new Definition(
    '%newsletter_manager.class%'
))->setFactoryService(
    'newsletter_factory'
)->setFactoryMethod(
    'get'
);

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.

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 get method in the previous example takes the templating service as an argument:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
parameters:
    # ...
    newsletter_manager.class: NewsletterManager
    newsletter_factory.class: NewsletterFactory
services:
    newsletter_factory:
        class:            "%newsletter_factory.class%"
    newsletter_manager:
        class:            "%newsletter_manager.class%"
        factory_service:  newsletter_factory
        factory_method:   get
        arguments:
            - "@templating"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<parameters>
    <!-- ... -->
    <parameter key="newsletter_manager.class">NewsletterManager</parameter>
    <parameter key="newsletter_factory.class">NewsletterFactory</parameter>
</parameters>

<services>
    <service id="newsletter_factory" class="%newsletter_factory.class%"/>
    <service id="newsletter_manager" 
             class="%newsletter_manager.class%"
             factory-service="newsletter_factory"
             factory-method="get"
    >
        <argument type="service" id="templating" />
    </service>
</services>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Symfony\Component\DependencyInjection\Definition;

// ...
$container->setParameter('newsletter_manager.class', 'NewsletterManager');
$container->setParameter('newsletter_factory.class', 'NewsletterFactory');

$container->setDefinition('newsletter_factory', new Definition(
    '%newsletter_factory.class%'
))
$container->setDefinition('newsletter_manager', new Definition(
    '%newsletter_manager.class%',
    array(new Reference('templating'))
))->setFactoryService(
    'newsletter_factory'
)->setFactoryMethod(
    'get'
);
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Put the code quality back at the heart of your project

Put the code quality back at the heart of your project

Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

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

Avatar of Nicolas Sauveur, a Symfony contributor

Thanks Nicolas Sauveur (@baishu) for being a Symfony contributor

3 commits • 84 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