Skip to content

Add wither behavior with PHP8 static return type #13619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions service_container/calls.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ instead of mutating the object they were called on::
{
private $logger;

/**
* @return static
*/
public function withLogger(LoggerInterface $logger)
{
$new = clone $this;
Expand Down Expand Up @@ -146,3 +143,19 @@ The configuration to tell the container it should do so would be like:

$container->register(MessageGenerator::class)
->addMethodCall('withLogger', [new Reference('logger')], true);

If autowire is enabled, you can also use annotations; with the previous exemple it would be::

/**
* @required
* @return static
*/
public function withLogger(LoggerInterface $logger)
{
$new = clone $this;
$new->logger = $logger;

return $new;
}

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.