Send a blank email to [email protected] to get a copy of this message
I'm looking for initial feedback on the following proposal.
Often, we have the following (fluent interface) setter method:
public function setName(?string $name): static
{
$this->name = $name;
return $this;
}
I propose to add "$this" as a return type, ie:
public function setName(?string $name): $this
{
$this->name = $name;
// implicit return $this;
}
public function setName(?string $name): $this
{
$this->name = $name;
// this would be an (compiler?) error
return $somethingOtherThanThis;
}
public function setName(?string $name): $this
{
$this->name = $name;
$self = $this;
// this would not be an error
return $self;
}
public function setName(?string $name): $this
{
$this->name = $name;
// technically useless, but not an error
return $this;
}
public function setName(?string $name): $this
{
$this->name = $name;
if ('foobar' === $name) {
// not an error to return early
return;
}
// do some other stuff
// any of the above legal examples
}
It should be obvious, but functions outside of a class context would not be
able to use this syntax.
BC:
There would be no BC breaks, as this syntax is currently invalid (Parse
error: syntax error, unexpected variable "$this").
Other Considerations:
With regards to reflection and inheritance, "$this" would be considered an
alias for "static".
Regards,
radar3301