Re: Allow dropping typehints during inheritance
| From: | Andrea Faulds | Date: | Thu, 05 Feb 2015 12:14:58 +0000 |
| Subject: | Re: Allow dropping typehints during inheritance | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81908@lists.php.net to get a copy of this message | ||
Hi Julien,
> On 5 Feb 2015, at 12:10, Julien Pauli <jpauli@php.net> wrote:
>
> If we allow larger type, why doesn't such code work ?
>
> interface A { }
> interface B extends A { }
>
> class C {
> public function foo(A $a) { }
> }
>
> class D extends C {
> public function foo(B $a) { } // E_STRICT
> }
>
> This is wrong IMO.
Well, firstly that’s the wrong way round: inheriting classes can only *increase* the range of
supported values, but you’ve done exactly the opposite.
But even if you fixed your code, you’d still have an error. This ispresumably because doing
anything other than simple invariance causes enormous problems related to compilation order and
autoloading, as we discovered with the Return Types RFC. I think that we’d probably support full
contravariance for parameters if we could.
However, we could easily support contravariance in type hint existence, as that doesn’t require
subclassing checks.
--
Andrea Faulds
http://ajf.me/