On Wed, 2014-09-17 at 16:52 +0200, Christian Stoller 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);
>     }

Here you say "any A can be passed at argument"

>     class D implements C {
>         public function foo(B $b) {

here you say "only a subset of A can be passed as argument"

Thus having

    function bar(C $c) {
        $a = new A();
        $c->foo($a);
    }

might work or might not work as $c might be an instance of D. Thus
breaks the Liskov Substitution Principle
http://en.wikipedia.org/wiki/Liskov_substitution_principle

doing it the other way round would work


    interface E {
         function foo(B $b);
    }

    class F implements E {
         public function foo(A $a) {
         }
    }

as any B satisfies the is-a requirement compared to A, so everything
valid for E::foo() is valid for F::foo(), too. Along with other subtypes
to A.

johannes



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

Reply via email to