Re: Proposal for php7 class method return
| From: | Marco Pivetta | Date: | Sat, 09 Jul 2016 07:03:47 +0000 |
| Subject: | Re: Proposal for php7 class method return | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-94439@lists.php.net to get a copy of this message | ||
Hey Daniel,
I've been playing around with
self for a while (mostly dealing with
code-generators and inheritance), and what I found is that it doesn't
really make sense as a type-hint, not even in interfaces.
Besides the type-work to be done to do `function setValue($value) :
myClass, what is the advantage of having self`?
Marco Pivetta
http://twitter.com/Ocramius
http://ocramius.github.com/
On 9 July 2016 at 08:47, Daniel Ciochiu <daniel@ciochiu.ro> wrote:
> Hi,
>
> I have a proposal that for a given method with a return type of <self>, if
> method does not return anything than it should it's instance. It will
> reduce code by only one line, but will improve consecutive method calls.
>
> class myClass
> {
> protected $value;
>
> function setValue($value) : self
> {
> $this->value = $value;
> }
>
> function doubleValue() : self
> {
> $this->value*=2;
> }
>
> function getValue() : self
> {
> return $this->value;
> }
> }
>
> $calculator = new myClass();
>
> $returnValue = $calculator->setValue(3)
> ->doubleValue()
> ->getValue();
>
> echo $returnValue;
>
> Any return defined in the methods body would override the return of the
> class instance.
>
> I am looking forward to your reaction.
> Thank you!
>
> DanielCiochiu
>