Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120436 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62080 invoked from network); 29 May 2023 19:09:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 May 2023 19:09:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 31F9E180511 for ; Mon, 29 May 2023 12:09:10 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 29 May 2023 12:09:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1685387348; bh=f6YFCLq8sB3Ka4FlSZntMKz4A93/EhwcjWLlfGg6os8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=bxqKfOO0bMKrFcexVWHxz6DIE2+FOg16+ZusUFs+U+qd+LvCVlPtenxBOjXP4UDK/ 5h97QdiEMfPGXRlh6f6MhyruWXVrQQLqiUOczgZaa4D1tvFiwS0EfycItotcjvPhCZ x9WPTNV5XRKGS7abwCFR2Mf9JLvMwkL7/uAeqJqukjVlmpQesx3nTXFBVngRcDBkJA LF2wGwzGxjcdE2K5YYlni7YHe5fwIp7bXtUYGW5V1fPpzhnSVoGOTm4EhRVr4W15XZ S2lwyzbHYBOzsBtIE43LzD6y/t86L+WxKUnDluVbJziqsDTdap8ZCq+K6xsXmJc+2e QILJtz0HLpbdQ== Message-ID: Date: Mon, 29 May 2023 21:09:07 +0200 MIME-Version: 1.0 Content-Language: en-US To: Sara Golemon Cc: PHP internals References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] RFC [Discussion]: Marking overridden methods (#[\Override]) From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=c3=bcsterhus?=) Hi On 5/23/23 17:47, Sara Golemon wrote: > I think targeting 8.3 is aggressive as we're less than a month from FF > (accounting for discussion and voting period). I didn't expect the proposal to need much of a discussion, as the functionality is known from existing programming languages and the proposed semantics are the direct result of how the existing LSP checks work. > > The first argument (about not impacting callers) for "why an attribute and > not a keyword" feels like it's tying itself into a knot to make a specious > point. To be clear: That is intended as a real argument that I gave some thought. The fact that it does not affect users of the method in question differs from the other keywords that are part of the method signature and thus I find it useful to have it visually separate. The visibility decides who can call the method, abstract/final add restrictions for classes extending the class in question and static/non-static decides how the method is called. The proposed override marker does nothing like that. > > // Intentional LSP violations > class A { > public function foo(): \SimpleXMLElement { return > simplexml_load_file("/tmp/file.xml"); } > } > class TestA extends A { > #[\Override(Override::IGNORE_RETURN_TYPE_VIOLATION)] > public function foo(): TestProxyClass { return TestProxy(parent::foo()); } > } > > LSP checks are super valuable for writing clean and well debuggable code, > but sometimes they get in the way of mocking or some other non-production > activity. This could provide a "get out of jail free card" for those > circumstances where you just want to tell the engine, "Shut up, I know what > I'm doing". > This is only tangentially to what my proposal intends to achieve and likely needs an entire discussion of its own. I believe it should be a separate thing, similarly to the #[\ReturnTypeWillChange] attribute. > > // Specific parent override > class A { > public function foo() { return 1; } > } > class B extends A { > // Not present in older versions of library, just added by maintainers. > public function foo() { return bar(); } > } > class C extends B { > // Errors because we're now overriding B::foo(), not A::foo(). > #[\Override(A::class)] > public function foo() { return parent::foo() + 1; } > } > > C was written at a time before B::foo() was implemented and makes > assumptions about its behavior. Then B adds their of foo() which breaks > those assumptions. C gets to know about this more quickly because the > upgrade breaks those assumptions. C should only use this subfeature in > places where the inheritance hierarchy matters (such as intentional LSP > violations). > This is something I could get behind. In fact one of the "complaints" that I read about Java's @Override is that it does not distinguish between a parent class and an interface. However the semantics are much less clear, here. What about multiple interfaces that (intentionally) define the same method or a parent class + an interface that both define the method? The parameter would likely need to be an array and emit an error if any class provides the method that is *not* listed, as well as if a class is listed that does not provide the method. However this likely gets a little funky if the method in your example was initially implemented in B and later added to A, because then A is not listed, but nothing changed about B which is the direct ancestor. Interestingly this would also allow to handle the case of "this method is not overriding anything" by using `#[\Override([])]`. I'd probably leave this as possible future scope. A new `?array $classes = null` (`class-string[]|null`) parameter could be added in the future without breaking anything. In any case I would need assistance with the implementation or someone else to implement that, because the added complexity is outside of what I'm comfortable with doing myself. Best regards Tim Düsterhus