Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Service Container
  4. How to work with Service Definition Objects
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Getting and Setting Service Definitions
  • Working with a Definition
    • Creating a New Definition
    • Class
    • Constructor Arguments
    • Method Calls
    • Requiring Files

How to work with Service Definition Objects

Edit this page

How to work with Service Definition Objects

Service definitions are the instructions describing how the container should build a service. They are not the actual services used by your applications. The container will create the actual class instances based on the configuration in the definition.

Normally, you would use YAML, XML or PHP to describe the service definitions. But if you're doing advanced things with the service container, like working with a Compiler Pass or creating a Dependency Injection Extension, you may need to work directly with the Definition objects that define how a service will be instantiated.

Getting and Setting Service Definitions

There are some helpful methods for working with the service definitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use Symfony\Component\DependencyInjection\Definition;

// finds out if there is an "app.mailer" definition
$container->hasDefinition('app.mailer');
// finds out if there is an "app.mailer" definition or alias
$container->has('app.mailer');

// gets the "app.user_config_manager" definition
$definition = $container->getDefinition('app.user_config_manager');
// gets the definition with the "app.user_config_manager" ID or alias
$definition = $container->findDefinition('app.user_config_manager');

// adds a new "app.number_generator" definition
$definition = new Definition(\App\NumberGenerator::class);
$container->setDefinition('app.number_generator', $definition);

// shortcut for the previous method
$container->register('app.number_generator', \App\NumberGenerator::class);

Working with a Definition

Creating a New Definition

In addition to manipulating and retrieving existing definitions, you can also define new service definitions with the Definition class.

Class

The first optional argument of the Definition class is the fully qualified class name of the object returned when the service is fetched from the container:

1
2
3
4
5
6
7
8
9
10
11
use App\Config\CustomConfigManager;
use App\Config\UserConfigManager;
use Symfony\Component\DependencyInjection\Definition;

$definition = new Definition(UserConfigManager::class);

// override the class
$definition->setClass(CustomConfigManager::class);

// get the class configured for this definition
$class = $definition->getClass();

Constructor Arguments

The second optional argument of the Definition class is an array with the arguments passed to the constructor of the object returned when the service is fetched from the container:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use App\Config\DoctrineConfigManager;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$definition = new Definition(DoctrineConfigManager::class, [
    new Reference('doctrine'), // a reference to another service
    '%app.config_table_name%',  // will be resolved to the value of a container parameter
]);

// gets all arguments configured for this definition
$constructorArguments = $definition->getArguments();

// gets a specific argument
$firstArgument = $definition->getArgument(0);

// adds a new named argument
// '$argumentName' = the name of the argument in the constructor, including the '$' symbol
$definition = $definition->setArgument('$argumentName', $argumentValue);

// adds a new argument
$definition->addArgument($argumentValue);

// replaces argument on a specific index (0 = first argument)
$definition->replaceArgument($index, $argument);

// replaces all previously configured arguments with the passed array
$definition->setArguments($arguments);

Caution

Don't use get() to get a service that you want to inject as constructor argument, the service is not yet available. Instead, use a Reference instance as shown above.

Method Calls

If the service you are working with uses setter injection then you can manipulate any method calls in the definitions as well:

1
2
3
4
5
6
7
8
9
10
11
// gets all configured method calls
$methodCalls = $definition->getMethodCalls();

// configures a new method call
$definition->addMethodCall('setLogger', [new Reference('logger')]);

// configures an immutable-setter
$definition->addMethodCall('withLogger', [new Reference('logger')], true);

// replaces all previously configured method calls with the passed array
$definition->setMethodCalls($methodCalls);

Tip

There are more examples of specific ways of working with definitions in the PHP code blocks of the Service Container articles such as Using a Factory to Create Services and How to Manage Common Dependencies with Parent Services.

Note

The methods here that change service definitions can only be used before the container is compiled. Once the container is compiled you cannot manipulate service definitions further. To learn more about compiling the container, see Compiling the Container.

Requiring Files

There might be use cases when you need to include another file just before the service itself gets loaded. To do so, you can use the setFile() method:

1
$definition->setFile('/src/path/to/file/foo.php');

Notice that Symfony will internally call the PHP statement require_once, which means that your file will be included only once per request.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:

    Symfony 5.4 is backed by

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    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).

    Symfony footer

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

    Avatar of Steffen Keuper, a Symfony contributor

    Thanks Steffen Keuper for being a Symfony contributor

    1 commit • 2 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