Skip to content

Commit 4b22d71

Browse files
committed
fix(code): fix on clone usage
1 parent e6b339c commit 4b22d71

File tree

1 file changed

+5
-35
lines changed

1 file changed

+5
-35
lines changed

service_container/injection_types.rst

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -261,21 +261,16 @@ this approach allow you to make a service immutable::
261261
{
262262
private $mailer;
263263
264-
public function __construct(?MailerInterface $mailer = null)
265-
{
266-
$this->mailer = $mailer;
267-
}
268-
269264
/**
270-
*
271-
* The @return is required.
272-
*
265+
* @required
273266
* @return static
274-
*
275267
*/
276268
public function withMailer(MailerInterface $mailer)
277269
{
278-
return new static($mailer);
270+
$new = clone $this;
271+
$new->mailer = $mailer;
272+
273+
return $new;
279274
}
280275

281276
// ...
@@ -327,31 +322,6 @@ which allow the container to return the newly created service:
327322
$container->register('app.newsletter_manager', NewsletterManager::class)
328323
->addMethodCall('withMailer', [new Reference('mailer')], true);
329324
330-
This example is pretty simple, let's imagine that you have a simple service
331-
which use a `LoggerAwareTrait`::
332-
333-
class RandomService
334-
{
335-
use LoggerAwareTrait;
336-
}
337-
338-
trait LoggerAwareTrait
339-
{
340-
private $logger;
341-
342-
/**
343-
* @return static
344-
*/
345-
public function withLogger(LoggerInterface $logger)
346-
{
347-
$new = clone $this;
348-
$new->logger = $logger;
349-
350-
return $new;
351-
}
352-
}
353-
354-
355325
This approach is useful if you need to configure your service according to your needs,
356326
so, what are the advantages?
357327

0 commit comments

Comments
 (0)