@@ -76,10 +76,11 @@ service is marked as autowired:
76
76
77
77
.. code-block :: php
78
78
79
+ use Acme\TwitterClient;
79
80
use Symfony\Component\DependencyInjection\Definition;
80
81
81
82
// ...
82
- $definition = new Definition('Acme\ TwitterClient' );
83
+ $definition = new Definition(TwitterClient::class );
83
84
$definition->setAutowired(true);
84
85
85
86
$container->setDefinition('twitter_client', $definition);
@@ -150,9 +151,8 @@ modifying the class depending of them.
150
151
151
152
To follow this best practice, constructor arguments must be typehinted with interfaces
152
153
and not concrete classes. It allows to replace easily the current implementation
153
- if necessary. It also allows to use other transformers.
154
-
155
- Let's introduce a ``TransformerInterface ``::
154
+ if necessary. It also allows to use other transformers. You can create a
155
+ ``TransformerInterface `` containing just one method (``transform() ``)::
156
156
157
157
namespace Acme;
158
158
@@ -164,18 +164,15 @@ Let's introduce a ``TransformerInterface``::
164
164
Then edit ``Rot13Transformer `` to make it implementing the new interface::
165
165
166
166
// ...
167
-
168
167
class Rot13Transformer implements TransformerInterface
169
-
170
- // ...
171
-
168
+ {
169
+ // ...
170
+ }
172
171
173
172
And update ``TwitterClient `` to depend of this new interface::
174
173
175
174
class TwitterClient
176
175
{
177
- // ...
178
-
179
176
public function __construct(TransformerInterface $transformer)
180
177
{
181
178
// ...
@@ -215,12 +212,13 @@ subsystem isn't able to find itself the interface implementation to register:
215
212
216
213
.. code-block :: php
217
214
215
+ use Acme\TwitterClient;
218
216
use Symfony\Component\DependencyInjection\Definition;
219
217
220
218
// ...
221
219
$container->register('rot13_transformer', 'Acme\Rot13Transformer');
222
220
223
- $clientDefinition = new Definition('Acme\ TwitterClient' );
221
+ $clientDefinition = new Definition(TwitterClient::class );
224
222
$clientDefinition->setAutowired(true);
225
223
$container->setDefinition('twitter_client', $clientDefinition);
226
224
@@ -353,23 +351,27 @@ and a Twitter client using it:
353
351
354
352
.. code-block :: php
355
353
354
+ use Acme\Rot13Transformer;
355
+ use Acme\TransformerInterface;
356
+ use Acme\TwitterClient;
357
+ use Acme\UppercaseTransformer;
356
358
use Symfony\Component\DependencyInjection\Reference;
357
359
use Symfony\Component\DependencyInjection\Definition;
358
360
359
361
// ...
360
- $rot13Definition = new Definition('Acme\ Rot13Transformer' );
361
- $rot13Definition->setAutowiringTypes(array('Acme\ TransformerInterface' ));
362
+ $rot13Definition = new Definition(Rot13Transformer::class );
363
+ $rot13Definition->setAutowiringTypes(array(TransformerInterface::class ));
362
364
$container->setDefinition('rot13_transformer', $rot13Definition);
363
365
364
- $clientDefinition = new Definition('Acme\ TwitterClient' );
366
+ $clientDefinition = new Definition(TwitterClient::class );
365
367
$clientDefinition->setAutowired(true);
366
368
$container->setDefinition('twitter_client', $clientDefinition);
367
369
368
- $uppercaseDefinition = new Definition('Acme\ UppercaseTransformer' );
370
+ $uppercaseDefinition = new Definition(UppercaseTransformer::class );
369
371
$uppercaseDefinition->setAutowired(true);
370
372
$container->setDefinition('uppercase_transformer', $uppercaseDefinition);
371
373
372
- $uppercaseClientDefinition = new Definition('Acme\ TwitterClient' , array(
374
+ $uppercaseClientDefinition = new Definition(TwitterClient::class , array(
373
375
new Reference('uppercase_transformer'),
374
376
));
375
377
$container->setDefinition('uppercase_twitter_client', $uppercaseClientDefinition);
0 commit comments