File tree Expand file tree Collapse file tree 1 file changed +26
-4
lines changed
Expand file tree Collapse file tree 1 file changed +26
-4
lines changed Original file line number Diff line number Diff 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+
333355This approach is useful if you need to configure your service according to your needs,
334356so, what are the advantages?
335357
You can’t perform that action at this time.
0 commit comments