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