Alexander Lisachenko wrote on 02/11/2015 11:12:
First definition declares exactly one single parameter, which can be
absent during the method call, so I can even write
public static function test() {}
Second definition defines zero or more arguments, so it can be also
described by the same signature:
public static function test() {}
OK, I got a bit confused with the different optional parameters, and
neither your explanation nor Andrea's quite did it for me.
But, I think you're right that this should be compatible, if the rule is
"any legal call on the parent should also be legal on the child".
Currently, all versions of PHP enforce the following:
- if parent accepts no parameters, child must accept zero, but could
accept one or more optional parameters
- if parent accepts zero or one (an optional parameter), child must
accept zero, must accept one, but could accept more (so, two optional
parameters is fine; no parameters at all is incompatible)
Thus this is acceptable:
class Foo {
public function test() {}
}
class Bar extends Foo {
public function test($foo = null) {}
}
But these are all not:
class Foo2 {
public function test($foo = null) {}
}
class Bar2 extends Foo2 {
// Doesn't accept one parameter
public function test() {}
}
class Baz2 extends Foo2 {
// Doesn't accept zero parameters
public function test($foo) {}
}
class Foo3 {
public function test() {}
}
class Bar3 extends Foo3 {
// Doesn't accept zero parameters
public function test($foo) {}
}
Variadic signatures count as "zero or more" when the parent accepts only
zero:
class Foo4 {
public function test() {}
}
class Bar4 extends Foo4 {
public function test(...$foo) {}
}
But where the parent accepts "zero or one", the following ought to be
compatible because calls with no parameters, and calls with one
parameter, would both be valid:
class Foo {
public function test($foo = null) {}
}
class Bar extends Foo {
public function test(...$foo) {}
}
In short, I agree with you that this warning is incorrect, but will
leave this here in case anybody else is equally confused (and becomes
any less so by reading this...)
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php