Hi, On Mon, Sep 19, 2011 at 10:40, Gustavo Lopes <glo...@nebm.ist.utl.pt> wrote:
> Em Sun, 18 Sep 2011 20:06:31 +0100, Stas Malyshev <smalys...@sugarcrm.com> > escreveu: > > On 9/18/11 11:23 AM, Nikita Popov wrote: >> >>> to tell you: I need this and that method accepting these and these >>> arguments to work correctly. If the signature isn't enforced, this >>> doesn't make sense anymore. You could just as well drop the signature >>> from abstract method definitions, as it's pointless then. >>> >> >> No it is not. The signature tells "this method would accept certain >> arguments". If you call it with these arguments, it would work. However, >> there's no promise to never extend the cases where it works - it goes >> contrary to the whole point of OOP to say "I will never loosen preconditions >> on my methods". >> > > While this is true, we should be a bit pragmatic here. > > If the subclass method specifies less parameters, it is very likely a > mistake. It's not usual (certainly it's not more frequent than a mistake > being the case) for arguments to be simply intentionally ignored. In fact, I > don't know of any popular language that implements this type of > contravariance. Plus, consider the case in which the superclass takes an > argument by reference: > > function increment(&$foo) { $foo = $foo + 1; } > > It is obvious that in this case a subclass override that takes no arguments > cannot possibly satisfy the contract of the original method. That is to say, > by ref arguments are also used for postconditions. > Sure, but you mix two things here, references would have to be handled specifically, and we would not allow to specify less args by ref. There is simply no reason for disallowing this with nornal args though. Imagine the following code: interface Foo { public function plop ($a,$b,$c,$d) } class A implements Foo { public function plop() { $args = func_get_args(); // ... } } This is perfectly valid PHP code, there is no valid usage of Foo that will not work with A... So to me this restriction seems really arbitrary. And I don;t believe missing an argument in the declaration of a method, using it in the body of the method and not realising it is so much of a common error that it would warrant this. > > That said, I think the only rules worth implementing are: > > * Allow arguments with a supertype. For instance, this should give no > warning: > <?php > class A {} > class B extends A {} > class C { function g(B $b) {} } > class D extends C { function g(A $a) { } } > > * Allow extra parameters with a default value (implemented): > > <?php > class C { function g($a) {} } > class D extends C { function g($a,$b=true) { } } > > -- > Gustavo Lopes > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Etienne Kneuss http://www.colder.ch