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. Components
  4. Dependency Injection
  5. Working with Container Service Definitions
  • 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

Working with Container Service Definitions

Edit this page

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

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

Working with Container Service Definitions

Getting and Setting Service Definitions

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

To find out if there is a definition for a service id:

1
$container->hasDefinition($serviceId);

This is useful if you only want to do something if a particular definition exists.

You can retrieve a definition with:

1
$container->getDefinition($serviceId);

or:

1
$container->findDefinition($serviceId);

which unlike getDefinition() also resolves aliases so if the $serviceId argument is an alias you will get the underlying definition.

The service definitions themselves are objects so if you retrieve a definition with these methods and make changes to it these will be reflected in the container. If, however, you are creating a new definition then you can add it to the container using:

1
$container->setDefinition($id, $definition);

Working with a definition

Creating a new definition

If you need to create a new definition rather than manipulate one retrieved from then container then the definition class is Definition.

Class

First up is the class of a definition, this is the class of the object returned when the service is requested from the container.

To find out what class is set for a definition:

1
$definition->getClass();

and to set a different class:

1
$definition->setClass($class); // Fully qualified class name as string

Constructor Arguments

To get an array of the constructor arguments for a definition you can use:

1
$definition->getArguments();

or to get a single argument by its position:

1
2
$definition->getArgument($index);
//e.g. $definition->getArguments(0) for the first argument

You can add a new argument to the end of the arguments array using:

1
$definition->addArgument($argument);

The argument can be a string, an array, a service parameter by using %parameter_name% or a service id by using :

1
2
3
4
5
use Symfony\Component\DependencyInjection\Reference;

// ...

$definition->addArgument(new Reference('service_id'));

In a similar way you can replace an already set argument by index using:

1
$definition->replaceArgument($index, $argument);

You can also replace all the arguments (or set some if there are none) with an array of arguments:

1
$definition->replaceArguments($arguments);

Method Calls

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

You can get an array of all the method calls with:

1
$definition->getMethodCalls();

Add a method call with:

1
$definition->addMethodCall($method, $arguments);

Where $method is the method name and $arguments is an array of the arguments to call the method with. The arguments can be strings, arrays, parameters or service ids as with the constructor arguments.

You can also replace any existing method calls with an array of new ones with:

1
$definition->setMethodCalls($methodCalls);

Tip

There are more examples of specific ways of working with definitions in the PHP code blocks of the configuration examples on pages such as Using a Factory to Create Services and Managing 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.

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

    Get your Sylius expertise recognized

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Symfony footer

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

    Avatar of iamvar, a Symfony contributor

    Thanks iamvar for being a Symfony contributor

    2 commits • 894 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