On Mon, Mar 2, 2020, at 11:00 AM, Matthew Weier O'Phinney wrote:

> >  2) `return self` is a breaking problem until 7.4, but gets better in 8.0.
> > 
> >  PSR-13, like a few other specs, has methods that return $this. They are 
> > currently untyped, just documented. I initially attempted to add `return 
> > self` to those methods in the v2/3 PR (#7), but that in fact breaks in PHP 
> > < 7.4, because of the weaker covariance. Example:
> > 
> > https://3v4l.org/ttop8
> > 
> >  Possible ways we could handle this:
> > 
> >  A) Do not add `return self` types until a 7.4-compatible version. That 
> > would mean a 3-step upgrade; v2 for parameter types (requires 7.2), v3 for 
> > other return types (requires 7.2), v4 for return self cases (requires 7.4).
> > 
> >  B) Skip the "return types other than self" step. So in PSR-13's case that 
> > would mean v2 for parameter types (requires PHP 7.2) and a v3 that adds all 
> > return types (requires 7.4).
> > 
> >  C) Wait for PHP 8 to release the return-type step, which is slated to have 
> > support for `return static`, which is what's really intended here anyway 
> > and is more accurate than `return self` entirely. This could be done with 
> > (CA) or without (CB) an intermediary step to add the non-self return values.
> > 
> >  I think my preference is for CB: Do parameter types now, then wait until 
> > December to do a v3 with `return static` and a PHP 8.0 dependency. As a 
> > matter of precedent, that would imply doing the same for PSR-7 and any 
> > other spec that has a documented `@return $this` or similar.
> 
> I tend to agree with option B as well. With most of the specs, there's 
> been uncertainty and confusion about what either the PHPDoc syntax or 
> PHP syntax would end up being, but the specs are very clear on the 
> behavior. Now that the PHP syntax is getting sorted, since `: self` 
> will work for PHP 7.4+, we can go with that, and then move to `: 
> static` at a later date.

To clarify, my preference was CB: Wait until :static in PHP 8 to add return 
types, since `static` is more correct than `self` in this case.  The object to 
be returned is contractually an instance of the called object, which would be a 
type that implements the interface, not just "anything that happens to 
implement this interface".  So calling (new LinkableImpl())->withLink(...) 
should return a LinkableImpl instance; returning a Response object that also 
implements EvolveableLinkProviderInterface would be wrong; static captures that 
fact, whereas self does not.

Since we know that PHP 8 will include a static return (the RFC passed), that 
only implies a delay, not a question of whether it will be doable or not.

--Larry Garfield

-- 
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to php-fig+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/c031e5d4-296e-48ca-915e-5ea3cf08469e%40www.fastmail.com.

Reply via email to