Skip to content

Commit 66527df

Browse files
committed
Add wither behavior with PHP8 static return type
1 parent 4c87c94 commit 66527df

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

service_container/calls.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ instead of mutating the object they were called on::
8888
{
8989
private $logger;
9090

91-
/**
92-
* @return static
93-
*/
9491
public function withLogger(LoggerInterface $logger)
9592
{
9693
$new = clone $this;
@@ -144,3 +141,21 @@ The configuration to tell the container it should do so would be like:
144141
145142
$container->register(MessageGenerator::class)
146143
->addMethodCall('withLogger', [new Reference('logger')], true);
144+
145+
If autowire is enabled, you can also use annotations; with the previous example it would be:
146+
147+
.. code-block:: php
148+
149+
/**
150+
* @required
151+
* @return static
152+
*/
153+
public function withLogger(LoggerInterface $logger)
154+
{
155+
$new = clone $this;
156+
$new->logger = $logger;
157+
158+
return $new;
159+
}
160+
161+
You can also leverage the PHP8 `static` return type instead of the `@return static` annotation. Note if you don't want a method with a PHP8 `static` return type and a `@required` annotation to behave as a wither, you can add a `@return $this` annotation to disable the *returns clone* feature.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy