Re: Implementing interface method with child class in parameter def (Bug #42330)

From: Date: Wed, 17 Sep 2014 15:01:27 +0000
Subject: Re: Implementing interface method with child class in parameter def (Bug #42330)
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-77294@lists.php.net to get a copy of this message
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

« previous php.internals (#77294) next »