Skip to content

Commit 211ea43

Browse files
committed
feature #13619 Add wither behavior with PHP8 static return type (l-vo)
This PR was submitted for the master branch but it was merged into the 5.1 branch instead. Discussion ---------- Add wither behavior with PHP8 static return type close #13614 Commits ------- 4bdab6f Add wither behavior with PHP8 static return type
2 parents 7370af6 + 4bdab6f commit 211ea43

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

service_container/calls.rst

+16-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ instead of mutating the object they were called on::
9090
{
9191
private $logger;
9292

93-
/**
94-
* @return static
95-
*/
9693
public function withLogger(LoggerInterface $logger)
9794
{
9895
$new = clone $this;
@@ -146,3 +143,19 @@ The configuration to tell the container it should do so would be like:
146143
147144
$container->register(MessageGenerator::class)
148145
->addMethodCall('withLogger', [new Reference('logger')], true);
146+
147+
If autowire is enabled, you can also use annotations; with the previous exemple it would be::
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)