Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106829 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57368 invoked from network); 2 Sep 2019 16:29:44 -0000 Received: from unknown (HELO mail.gna.ch) (62.12.172.119) by pb1.pair.com with SMTP; 2 Sep 2019 16:29:44 -0000 Received: from [10.183.2.190] (unknown [217.192.174.36]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by darkcity.gna.ch (Postfix) with ESMTPSA id 89C0920C29 for ; Mon, 2 Sep 2019 16:03:08 +0200 (CEST) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Message-ID: Date: Mon, 2 Sep 2019 16:03:08 +0200 To: PHP internals X-Mailer: Apple Mail (2.3445.104.11) Subject: Mixed mode methods From: cschneid@cschneid.com (Christian Schneider) Hi internals, While some people are trying to make PHP a stricter language I'm also = interested in making it a more flexible language by allowing to opt-in = to advanced features for people who want it. Please don't shoot this down just because you are not the target = audience of such a feature ;-) Toying around with PHP 8 I noticed that it is now impossible to have = methods which can be called both statically and in an object. This proposal brings back mixed mode methods both for extensions and a = new syntax for user-land functions to explicitly allow it. Motivation: - Make migration of tried-and-tested mixed mode methods easier while = having to explicitly declare it. So yes, unchanged code gets the new = stricter semantics but updating code to the new (and from now on = well-defined) behaviour can be done by simply extending the function = definition. Having to rewrite such code to two different method names = can be tedious and error-prone without big benefit. - Allowing mixed mode methods as a form of name-overload in cases having = to have two separate names for static/non-static is a WTF. - Reviving things like $elements =3D DOMDocument::loadXML($html)->childNodes; instead of having to rewrite them to something like ($dom =3D new DOMDocument)->loadXML($html); $elements =3D $dom->childNodes; by adding ZEND_ACC_ALLOW_STATIC to the method signature = (ext/dom/document.c currently still contains code to handle both cases). A first shot at an implementation was done using the syntax ?static and = using isset($this) to determine the current mode: class A { var $dynamic =3D 'dynamic'; ?static function mixedmodefn() { return isset($this) ? = $this->dynamic : 'static'; } } allowing both (new A)->mixedmodefn() and A::mixedmodefn(). The implementation (including a test) can be found at = https://github.com/php/php-src/compare/master...chschneider:optional_stati= c If people are interested I could create an RFC for this. Regards, - Chris