@@ -266,9 +266,19 @@ this approach allow you to make a service immutable::
266266 $this->mailer = $mailer;
267267 }
268268
269+ /**
270+ *
271+ * The @return is required.
272+ *
273+ * @return static
274+ *
275+ */
269276 public function withMailer(MailerInterface $mailer)
270277 {
271- return new static($mailer);
278+ $new = clone $this;
279+ $new->mailer = $mailer;
280+
281+ return $new
272282 }
273283
274284 // ...
@@ -303,9 +313,8 @@ which allow the container to return the newly created service:
303313 <!-- ... -->
304314
305315 <service id =" app.newsletter_manager" class =" App\Mail\NewsletterManager" >
306- <call method =" withMailer" >
316+ <call method =" withMailer" use-result =true >
307317 <argument type =" service" id =" mailer" />
308- <use-result />
309318 </call >
310319 </service >
311320 </services >
@@ -319,7 +328,7 @@ which allow the container to return the newly created service:
319328
320329 // ...
321330 $container->register('app.newsletter_manager', NewsletterManager::class)
322- ->addMethodCall('withMailer', [new Reference('mailer')], 'use_result' => true);
331+ ->addMethodCall('withMailer', [new Reference('mailer')], true);
323332
324333 This approach is useful if you need to configure your service according to your needs,
325334so, what are the advantages?
@@ -329,3 +338,9 @@ so, what are the advantages?
329338
330339* You can easily change the injected service as long as it respect the interface
331340 asked by the initial service.
341+
342+ * It allow you to get rid of factory usages which can lead a more complex code
343+
344+ The disadvantages of this approach are:
345+
346+ *
0 commit comments