@@ -9,30 +9,33 @@ How to Create Service Aliases and Mark Services as Private
99Marking Services as Public / Private
1010------------------------------------
1111
12- When defining services, you'll usually want to be able to access these definitions
13- within your application code. These services are called *public *. For
14- example, the ``doctrine `` service is a public service. This means that you can
15- fetch it from the container using the ``get() `` method::
12+ When defining a service, it can be made to be *public * or *private *. If a service
13+ is *public *, it means that you can access it directly from the container at runtime.
14+ For example, the ``doctrine `` service is a public service::
1615
16+ // only public services can be accessed in this way
1717 $doctrine = $container->get('doctrine');
1818
19- In some cases, a service *only * exists to be injected into another service
20- and is *not * intended to be fetched directly from the container as shown
21- above.
19+ But typically, services are accessed using :ref: `dependency injection <services-constructor-injection >`.
20+ And in this case, those services do *not * need to be public.
2221
2322.. _inlined-private-services :
2423
25- In these cases, to get a minor performance boost and ensure the service will not
26- be retrieved directly from the container, you can set the service to be *not *
27- public (i.e. private):
24+ So unless you *specifically * need to access a service directly from the container
25+ via ``$container->get() ``, the best-practice is to make your services *private *.
26+ In fact, the :ref: `default services.yml configuration <container-public >` configures
27+ all services to be private by default.
28+
29+ You can also control the ``public `` option on a service-by-service basis:
2830
2931.. configuration-block ::
3032
3133 .. code-block :: yaml
3234
3335 services :
34- foo :
35- class : Example\Foo
36+ # ...
37+
38+ AppBundle\Service\Foo :
3639 public : false
3740
3841 .. code-block :: xml
@@ -43,29 +46,30 @@ public (i.e. private):
4346 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
4447
4548 <services >
46- <service id =" foo " class = " Example \Foo" public =" false" />
49+ <service id =" AppBundle\Service \Foo" public =" false" />
4750 </services >
4851 </container >
4952
5053 .. code-block :: php
5154
52- use Example \Foo;
55+ use AppBundle\Service \Foo;
5356
54- $container->register('foo', Foo::class)
57+ $container->register(Foo::class)
5558 ->setPublic(false);
5659
57- What makes private services special is that, since the container knows that the
58- service will never be requested from outside, it can optimize whether and how it
59- is instanciated. This increases the container's performance.
60+ Private services are special because they allow the container to optimize whether
61+ and how they are instantiated. This increases the container's performance.
6062
6163Now that the service is private, you *should not * fetch the service directly
6264from the container::
6365
64- $container->get('foo');
66+ use AppBundle\Service\Foo;
67+
68+ $container->get(Foo::class);
6569
6670This *may or may not work *, depending on how the container has optimized the
67- service instanciation and, even in the cases where it works, is
68- deprecated. Simply said: A service can be marked as private if you do not want
71+ service instantiation and, even in the cases where it works, is
72+ deprecated. Simply said: A service should be marked as private if you do not want
6973to access it directly from your code.
7074
7175However, if a service has been marked as private, you can still alias it
@@ -76,6 +80,8 @@ However, if a service has been marked as private, you can still alias it
7680 Services are by default public, but it's considered a good practice to mark
7781 as much services private as possible.
7882
83+ .. _services-alias :
84+
7985Aliasing
8086--------
8187
@@ -88,11 +94,13 @@ services.
8894 .. code-block :: yaml
8995
9096 services :
91- app.phpmailer :
92- class : AppBundle\Mail\PhpMailer
97+ # ...
98+ AppBundle\Mail\PhpMailer :
99+ public : false
93100
94101 app.mailer :
95- alias : app.phpmailer
102+ alias : AppBundle\Mail\PhpMailer
103+ public : true
96104
97105 .. code-block :: xml
98106
@@ -102,22 +110,23 @@ services.
102110 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
103111
104112 <services >
105- <service id = " app.phpmailer " class =" AppBundle\Mail\PhpMailer" />
113+ <service class =" AppBundle\Mail\PhpMailer" public = " false " />
106114
107- <service id =" app.mailer" alias =" app.phpmailer " />
115+ <service id =" app.mailer" alias =" AppBundle\Mail\PhpMailer " />
108116 </services >
109117 </container >
110118
111119 .. code-block :: php
112120
113121 use AppBundle\Mail\PhpMailer;
114122
115- $container->register('app.phpmailer', PhpMailer::class);
123+ $container->register(PhpMailer::class)
124+ ->setPublic(false);
116125
117- $container->setAlias('app.mailer', 'app.phpmailer' );
126+ $container->setAlias('app.mailer', PhpMailer::class );
118127
119128 This means that when using the container directly, you can access the
120- ``app.phpmailer `` service by asking for the ``app.mailer `` service like this::
129+ ``PhpMailer `` service by asking for the ``app.mailer `` service like this::
121130
122131 $container->get('app.mailer'); // Would return a PhpMailer instance
123132
@@ -141,8 +150,7 @@ or you decided not to maintain it anymore), you can deprecate its definition:
141150
142151 .. code-block :: yaml
143152
144- acme.my_service :
145- class : ...
153+ AppBundle\Service\OldService :
146154 deprecated : The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.
147155
148156 .. code-block :: xml
@@ -153,16 +161,18 @@ or you decided not to maintain it anymore), you can deprecate its definition:
153161 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
154162
155163 <services >
156- <service id =" acme.my_service " class = " ... " >
164+ <service id =" AppBundle\Service\OldService " >
157165 <deprecated >The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.</deprecated >
158166 </service >
159167 </services >
160168 </container >
161169
162170 .. code-block :: php
163171
172+ use AppBundle\Service\OldService;
173+
164174 $container
165- ->register('acme.my_service', '...' )
175+ ->register(OldService::class )
166176 ->setDeprecated(
167177 true,
168178 'The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.'
@@ -189,6 +199,6 @@ occurrence of the ``%service_id%`` placeholder in your template.
189199 deprecated, until when it will be maintained and the alternative services
190200 to use (if any).
191201
192- For service decorators (see above ), if the definition does not modify the
193- deprecated status, it will inherit the status from the definition that is
194- decorated.
202+ For service decorators (see :doc: ` /service_container/service_decoration ` ), if the
203+ definition does not modify the deprecated status, it will inherit the status from
204+ the definition that is decorated.
0 commit comments