Re: Allow dropping typehints during inheritance

From: Date: Thu, 05 Feb 2015 17:34:12 +0000
Subject: Re: Allow dropping typehints during inheritance
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-81949@lists.php.net to get a copy of this message
Hi! > 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. This shouldn't work - it means that if you have D object, you should be able to call foo on it with any A parameter. However, your definition says not every A parameter would work, but only one that is actually B (i.e. class that implements A but not B is not going to work anymore). That means D violates C's contract by rejecting some calls that C accepted. -- Stas Malyshev smalyshev@gmail.com

« previous php.internals (#81949) next »