Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:55518 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25251 invoked from network); 19 Sep 2011 09:01:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Sep 2011 09:01:37 -0000 Authentication-Results: pb1.pair.com smtp.mail=ekneuss@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ekneuss@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.42 as permitted sender) X-PHP-List-Original-Sender: ekneuss@gmail.com X-Host-Fingerprint: 209.85.214.42 mail-bw0-f42.google.com Received: from [209.85.214.42] ([209.85.214.42:46889] helo=mail-bw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F4/95-14600-0F4077E4 for ; Mon, 19 Sep 2011 05:01:36 -0400 Received: by bkar4 with SMTP id r4so5093662bka.29 for ; Mon, 19 Sep 2011 02:01:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=+yUweRPEBBQrRQP71XfBgo2l211/d8R7AM7ZYuEJD6c=; b=wn2kL5EOT/OT0lzljTyk78HZ4NFDMsR6I1JjlIw6m1swq+NyePScCdnxuLuxXsaulk GXvo3V38fDZ2itdVmksA4HbE25zExR7JCTZBGKoFClAu8FHJ7Tp3kTXeZQkMEBndTOy8 AMmX4QJA3zrGgcqVtt49nJMZeu7C5dAlPzU1Q= MIME-Version: 1.0 Received: by 10.204.141.80 with SMTP id l16mr645988bku.183.1316422893162; Mon, 19 Sep 2011 02:01:33 -0700 (PDT) Sender: ekneuss@gmail.com Received: by 10.204.59.68 with HTTP; Mon, 19 Sep 2011 02:01:32 -0700 (PDT) In-Reply-To: References: <4E74E5A0.2030006@sugarcrm.com> <4E76320F.6010904@sugarcrm.com> <4E764137.9080507@sugarcrm.com> Date: Mon, 19 Sep 2011 11:01:32 +0200 X-Google-Sender-Auth: t9lK48P_W_p7j5ggChW6CxeRgMA Message-ID: To: Gustavo Lopes Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=001517448364ebd13c04ad4796e5 Subject: Re: [PHP-DEV] __constructor parameter limitations. From: colder@php.net (Etienne Kneuss) --001517448364ebd13c04ad4796e5 Content-Type: text/plain; charset=UTF-8 Hi, On Mon, Sep 19, 2011 at 10:40, Gustavo Lopes wrote: > Em Sun, 18 Sep 2011 20:06:31 +0100, Stas Malyshev > 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: > 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): > > 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 --001517448364ebd13c04ad4796e5--