How to Manage Common Dependencies with Parent Services

Version: 3.0
Edit this page

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

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

How to Manage Common Dependencies with Parent Services

As you add more functionality to your application, you may well start to have related classes that share some of the same dependencies. For example, you may have multiple repository classes which need the doctrine.entity_manager service and an optional logger service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// src/AppBundle/Doctrine/BaseDoctrineRepository.php
namespace AppBundle\Doctrine;

// ...
abstract class BaseDoctrineRepository
{
    protected $entityManager;
    protected $logger;

    public function __construct(ObjectManager $manager)
    {
        $this->entityManager = $manager;
    }

    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    // ...
}

Just as you use PHP inheritance to avoid duplication in your PHP code, the service container allows you to extend parent services in order to avoid duplicated service definitions:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
services:
    app.base_doctrine_repository:
        # as no class is configured, the parent service MUST be abstract
        abstract:  true
        arguments: ['@doctrine.entity_manager']
        calls:
            - [setLogger, ['@logger']]

    app.user_repository:
        class:  AppBundle\Doctrine\DoctrineUserRepository
        # extend the app.base_doctrine_repository service
        parent: app.base_doctrine_repository

    app.post_repository:
        class:  AppBundle\Doctrine\DoctrinePostRepository
        parent: app.base_doctrine_repository

    # ...

In this context, having a parent service implies that the arguments and method calls of the parent service should be used for the child services. Specifically, the EntityManager will be injected and setLogger() will be called when app.user_repository is instantiated.

Caution

The shared, abstract and tags attributes are not inherited from parent services.

Tip

In the examples shown, the classes sharing the same configuration also extend from the same parent class in PHP. This isn't necessary at all. You can just extract common parts of similar service definitions into a parent service without also extending a parent class in PHP.

Overriding Parent Dependencies

There may be times where you want to override what service is injected for one child service only. You can override most settings by simply specifying it in the child class:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
services:
    # ...

    app.user_repository:
        class:  AppBundle\Doctrine\DoctrineUserRepository
        parent: app.base_doctrine_repository

        # overrides the public setting of the parent service
        public: false

        # appends the '@app.username_checker' argument to the parent
        # argument list
        arguments: ['@app.username_checker']

    app.post_repository:
        class:  AppBundle\Doctrine\DoctrinePostRepository
        parent: app.base_doctrine_repository

        # overrides the first argument (using the special index_N key)
        arguments:
            index_0: '@doctrine.custom_entity_manager'
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.