Skip to content

Conversation

@dmanifestor
Copy link

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

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);
@carsonbot carsonbot changed the title Symfony Service Definition Objects Symfony Service Definition Objects Dec 14, 2025
@xabbuh
Copy link
Member

xabbuh commented Dec 15, 2025

I don’t know what you are trying here, but this is not the right fix. Let me close here and we can always reopen when you have something ready. If you need help, don’t hesitate to ask.

@xabbuh xabbuh closed this Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants