Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106830 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59979 invoked from network); 2 Sep 2019 16:40:49 -0000 Received: from unknown (HELO mail-lf1-f43.google.com) (209.85.167.43) by pb1.pair.com with SMTP; 2 Sep 2019 16:40:49 -0000 Received: by mail-lf1-f43.google.com with SMTP id q27so10491090lfo.10 for ; Mon, 02 Sep 2019 07:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=csmSQOxuadZUKzLSAEPj1zHgN3SbMlyIPsvvbinY0u0=; b=XsA6TTi1gX3rbAniUJ+bUeyGNRbcONyWgYrIqCZZ5lGMVyCRUyqVsQ5FWgaT92a0et hJWCTIsUiKOep2t3Nf/esHEE4Pus1wWEKmZ6sRXQQiEKqXyeH4Kamci/jzH9g2fv7AJg tSLOc0eOoJw6zei3RKZBeN5t0RJUtDs9rwYGdlzDaBBVUg4Dj3Hc0SERyo7kOdmi05xw gYHnzL/g0YDuPZMHzK5MMVztll2sAfWI+XzVpQFD7s5dHI/X2A6AWq1J4j8NwyxbwP+u xvG5bUskq21j0p4lSR7aVigvvwW8JUaLknvG0lZqvSYD6d50fSJDAjRAaL/6yub39f4B efIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=csmSQOxuadZUKzLSAEPj1zHgN3SbMlyIPsvvbinY0u0=; b=jsSKSc5zVPydI1LJczAeT+19PlRDgNkoh4gRzSqWgN7DSnoG9vyYmrYWNM0GsZEgsU N2ccqzTYCtrX8P95Xgomu/Adhx40clTyW0F0DesiAA3RrZaBBpsWbKx2f8s+tXYm7U8h BI4jUZ6YkyOLarZliaehTqvXRx9k5ofSQQUPzJr0+YijEEhDMetx9LV4FLffc2+bj5Np 9HpKa9TVKzyGcQ9p9sJ0oCSCh+4RAW+V6W+FDtScCE6yzKuoIAB+zwjzjIiwP5zWe1Mu Junxo4N2DgdjEL4EyTcilBe7skLZ2HavkOSxM+9sUnsdfHKJ3qOEm+oP6Adgl89AHZxo jNGg== X-Gm-Message-State: APjAAAWZ8oH5g79i2peNcc0Sj9laoC5Sauxz+2pWsnOV4U7CcjfMNero H5XfTyJdg63hX7876+/2jO9Y40bI2+CXA6/y/w2ykIHT X-Google-Smtp-Source: APXvYqxbmMfStDJ0AlBDTZEgCjgi7ScBt6BbVelMO29lRQgpo1N58/NRZiQfocvRk7+91Ex7u8UUnv1X7FrKveRMoR0= X-Received: by 2002:ac2:5bde:: with SMTP id u30mr16235515lfn.59.1567433653670; Mon, 02 Sep 2019 07:14:13 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 2 Sep 2019 16:13:57 +0200 Message-ID: To: Christian Schneider Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000022d53c05919296c8" Subject: Re: [PHP-DEV] Mixed mode methods From: nikita.ppv@gmail.com (Nikita Popov) --00000000000022d53c05919296c8 Content-Type: text/plain; charset="UTF-8" On Mon, Sep 2, 2019 at 4:03 PM Christian Schneider wrote: > 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 = DOMDocument::loadXML($html)->childNodes; > instead of having to rewrite them to something like > ($dom = new DOMDocument)->loadXML($html); > $elements = $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 = '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_static > > If people are interested I could create an RFC for this. > I'm not in favor of this, but I'll also say that I don't hate it either. The big problem with what we had before was that any random instance method could also be used as a static method, which in 99% of the cases was not intended and would not work. Additionally, the engine had to always account for this edge-case possibility and perform extra checks that only rarely did something useful. Your proposal to use an explicit ?static annotation leaves this as a questionable coding pattern, but I don't see any big technical problems with it. Regards, Nikita --00000000000022d53c05919296c8--