Skip to content

Commit e6b339c

Browse files
committed
feat(code): small fix on static usage & clone usage added
1 parent 3f9d840 commit e6b339c

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

service_container/injection_types.rst

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,7 @@ this approach allow you to make a service immutable::
275275
*/
276276
public function withMailer(MailerInterface $mailer)
277277
{
278-
$new = clone $this;
279-
$new->mailer = $mailer;
280-
281-
return $new
278+
return new static($mailer);
282279
}
283280

284281
// ...
@@ -330,6 +327,31 @@ which allow the container to return the newly created service:
330327
$container->register('app.newsletter_manager', NewsletterManager::class)
331328
->addMethodCall('withMailer', [new Reference('mailer')], true);
332329
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+
333355
This approach is useful if you need to configure your service according to your needs,
334356
so, what are the advantages?
335357

0 commit comments

Comments
 (0)