44Working with Container Service Definitions
55==========================================
66
7+ Service definitions are the instructions describing how the container should
8+ build a service. They are not the actual services used by your applications
9+
710Getting and Setting Service Definitions
811---------------------------------------
912
@@ -32,34 +35,68 @@ with these methods and make changes to it these will be reflected in the
3235container. If, however, you are creating a new definition then you can add
3336it to the container using::
3437
35- $container->setDefinition($id, $definition);
38+ use Symfony\Component\DependencyInjection\Definition;
39+
40+ $definition = new Definition('Acme\Service\MyService');
41+ $container->setDefinition('acme.my_service', $definition);
42+
43+ .. tip ::
44+
45+ Registering service definitions is so common that the container provides a
46+ shortcut method called ``register() ``::
47+
48+ $container->register('acme.my_service', 'Acme\Service\MyService');
3649
3750Working with a Definition
3851-------------------------
3952
4053Creating a New Definition
4154~~~~~~~~~~~~~~~~~~~~~~~~~
4255
43- If you need to create a new definition rather than manipulate one retrieved
44- from the container then the definition class is :class: `Symfony\\ Component\\ DependencyInjection\\ Definition `.
56+ In addition to manipulating and retrieving existing definitions, you can also
57+ define new service definitions with the :class: `Symfony\\ Component\\ DependencyInjection\\ Definition `
58+ class.
4559
4660Class
4761~~~~~
4862
49- First up is the class of a definition, this is the class of the object returned
50- when the service is requested from the container.
63+ The first optional argument of the `` Definition `` class is the fully qualified
64+ class name of the object returned when the service is fetched from the container::
5165
52- To find out what class is set for a definition::
66+ use Symfony\Component\DependencyInjection\Definition;
67+
68+ $definition = new Definition('Acme\Service\MyService');
69+
70+ If the class is unknown when instantiating the ``Definition `` class, use the
71+ ``setClass() `` method to set it later::
5372
54- $definition->getClass( );
73+ $definition->setClass('Acme\Service\MyService' );
5574
56- and to set a different class ::
75+ To find out what class is set for a definition ::
5776
58- $definition->setClass($class); // Fully qualified class name as string
77+ $class = $definition->getClass();
78+ // $class = 'Acme\Service\MyService'
5979
6080Constructor Arguments
6181~~~~~~~~~~~~~~~~~~~~~
6282
83+ The second optional argument of the ``Definition `` class is an array with the
84+ arguments passed to the constructor of the object returned when the service is
85+ fetched from the container::
86+
87+ use Symfony\Component\DependencyInjection\Definition;
88+
89+ $definition = new Definition(
90+ 'Acme\Service\MyService',
91+ array('argument1' => 'value1', 'argument2' => 'value2')
92+ );
93+
94+ If the arguments are unknown when instantiating the ``Definition `` class or if
95+ you want to add new arguments, use the ``addArgument() `` method, which adds them
96+ at the end of the arguments array::
97+
98+ $definition->addArgument($argument);
99+
63100To get an array of the constructor arguments for a definition you can use::
64101
65102 $definition->getArguments();
@@ -69,12 +106,16 @@ or to get a single argument by its position::
69106 $definition->getArgument($index);
70107 // e.g. $definition->getArgument(0) for the first argument
71108
72- You can add a new argument to the end of the arguments array using::
109+ The argument can be a string, an array or a service parameter by using the
110+ ``%parameter_name% `` syntax::
73111
74- $definition->addArgument($argument );
112+ $definition->addArgument('%kernel_debug%' );
75113
76- The argument can be a string, an array, a service parameter by using ``%parameter_name% ``
77- or a service id by using::
114+ If the argument is another service, don't use the ``get() `` method to fetch it,
115+ because it won't be available when defining services. Instead, use the
116+ :class: `Symfony\\ Component\\ DependencyInjection\\ Reference ` class to get a
117+ reference to the service which will be available once the service container is
118+ fully built::
78119
79120 use Symfony\Component\DependencyInjection\Reference;
80121
@@ -139,4 +180,3 @@ the service itself gets loaded. To do so, you can use the
139180
140181Notice that Symfony will internally call the PHP statement ``require_once ``,
141182which means that your file will be included only once per request.
142-
0 commit comments