Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89046 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 13043 invoked from network); 2 Nov 2015 19:44:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Nov 2015 19:44:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=dev@mabe.berlin; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=dev@mabe.berlin; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mabe.berlin from 80.237.132.167 cause and error) X-PHP-List-Original-Sender: dev@mabe.berlin X-Host-Fingerprint: 80.237.132.167 wp160.webpack.hosteurope.de Received: from [80.237.132.167] ([80.237.132.167:35081] helo=wp160.webpack.hosteurope.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B7/09-42726-F0DB7365 for ; Mon, 02 Nov 2015 14:44:16 -0500 Received: from dslb-178-008-195-049.178.008.pools.vodafone-ip.de ([178.8.195.49] helo=[192.168.178.46]); authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) id 1ZtL1Y-0001oL-6v; Mon, 02 Nov 2015 20:44:12 +0100 To: internals@lists.php.net References: <56372B62.1000206@gmail.com> <56379241.2080900@gmail.com> Message-ID: <5637BD0B.1000805@mabe.berlin> Date: Mon, 2 Nov 2015 20:44:11 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <56379241.2080900@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-bounce-key: webpack.hosteurope.de;dev@mabe.berlin;1446493456;a3c6c69f; Subject: Re: [PHP-DEV] [Question] Variadic method signature compatibility between 5.6 and 7.0 From: dev@mabe.berlin (Marc Bennewitz) Hi Rowan, On 11/02/2015 05:41 PM, Rowan Collins wrote: > 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...) > In my opinion the warning is valid as the one method defines one optional argument and the overwritten method would accept no or more arguments where the no arguments is wrong: https://3v4l.org/6eMiu class Foo { public function test($foo = null) {} } class Bar extends Foo { public function test() {} } The first defined argument gets more interesting if it defines a type-hint what is not possible to define as variadic: https://3v4l.org/HQdvI class Foo { public function test(Foo $foo = null) {} } class Bar extends Foo { public function test(...$bar) {} } Additionally this issue can simply be addressed by: https://3v4l.org/74r3K class Foo { public function test(Foo $foo = null) {} } class Bar extends Foo { public function test(Foo $foo = null, ...$bar) {} } > Regards, Marc