On Wed, Sep 17, 2014 at 8:52 AM, Christian Stoller <stol...@leonex.de> wrote:
> Hello all,
>
> I hope the subject is not misleading. Please look at the following code:
>
>     <?php
>     class A { }
>
>     class B extends A { }
>
>     interface C {
>         function foo(A $a);
>     }
>
>     class D implements C {
>         public function foo(B $b) {
>
>         }
>     }
>
> This code produces a "Fatal error: Declaration of D::foo() must be
> compatible with C::foo(A $a) in /xyz/inheritance.php on line 10"
> (see http://3v4l.org/l2M0f).
>
> I don't get the reason for that behavior (and I could not find any
> documentation about that, at least not at
> http://php.net/manual/en/language.oop5.typehinting.php).
>
> I have already found https://bugs.php.net/bug.php?id=42330 but Derick's
> response does not help me and the linked file cannot be accessed
> anymore.
>
> I'd say that it is absolutely legal to define a more specialized
> type in a child or implementing class, or would this have any bad
> side effects?

This is because parameters are not covariant, they are usually
invariant or contravariant.

Almost certainly you have no clue what that actually means; it's okay,
most people don't. Just go read and learn about them and learn for
yourself why parameter types are not covariant.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to