Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113608 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 64415 invoked from network); 19 Mar 2021 13:08:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Mar 2021 13:08:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3EDA61804E2 for ; Fri, 19 Mar 2021 06:03:09 -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,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 19 Mar 2021 06:03:08 -0700 (PDT) Received: by mail-lj1-f179.google.com with SMTP id a1so11892018ljp.2 for ; Fri, 19 Mar 2021 06:03:08 -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=rrUbS8ucYLNnKDDTQNBuzZUt9jOX3SSPJ/iUM/p6oyA=; b=CKYu/IKHwUbbVfmXL3XJiJcGUZtZMmKchAhM9W5A7dNwsyUJXDp6r5G5vj0yHbbCwG +Eg4kRapZI+bEyPCaHvR/ESFnZOlLQjALSE2sVPVcudmKsj/Eg3rEgrtV5cY8lod+qnF VOiALfgC0EOj5dLwhNiulfk7IMAvW45FyoCpweeD2leP9IureETrRlhRfsphNpA1BEWM YgAxyxziEvVrWdWB3cCfg7zvewKxk3fmBb7qH6QtqbG7GE8yRf1VyAvCJL125/1GAPbP JmgHlGu5l5RlXjUuw4sEDhmhs2X8xKlW/3fsN3WlDdus9jedfla1AiWPYeIt109Y33Rp erdQ== 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=rrUbS8ucYLNnKDDTQNBuzZUt9jOX3SSPJ/iUM/p6oyA=; b=Lx++6KlL0Eny4f+5P2dMdxyied+BkdbPxM8Szl2K80i3p1yLpuJrZdgzxoyi2TcLYk xO/r2FD5iW+P6X7L2uoiZRCmyivUbPzN6CbgB51Dov12iNrSAn09Z2PFDBWYcIRr/khJ SEBTTOB8LbuTB0taF0nEDmYqAa5VgiJeDO0hCHo8/Ayl5VcI7ZPwvSUfCgoix4iDqN5k hy7w7Nf8/D5F2a7khkux+6T2Dwkv0UBMQ9JnFPT1D2CRXBYLDWf96XFqIT8BmbKm1PYE SY5hUPv8NhNc/6vpdk4kZwmQRuWxmzb/GL4tZXP5XA2SyVqLvygh3gBek0S8huXngYHI DCNA== X-Gm-Message-State: AOAM530je431YXGPVioyi7fa5my44/bYHKrXOy0lGi1PL5oV4genYpmf ///FLI9vBaAF7tEoAnELpBZkyeLU1Xppvn4ze1w= X-Google-Smtp-Source: ABdhPJzmGer0wYMwBN+nlaBn17AtDbbJzY6SPf3sqeQvhGiv+oM7ZJXdmLjdCgl/zxvANOEzjY7nat4iyG7gk2YOdx8= X-Received: by 2002:a2e:978b:: with SMTP id y11mr866430lji.452.1616158985643; Fri, 19 Mar 2021 06:03:05 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 19 Mar 2021 14:02:49 +0100 Message-ID: To: =?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?= Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000003d697605bde35676" Subject: Re: [PHP-DEV] [RFC] New in initializers From: nikita.ppv@gmail.com (Nikita Popov) --0000000000003d697605bde35676 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Mar 19, 2021 at 12:57 PM Nikita Popov wrote: > On Fri, Mar 19, 2021 at 12:22 PM Nikita Popov > wrote: > >> On Wed, Mar 3, 2021 at 7:04 PM Alexandru P=C4=83tr=C4=83nescu >> wrote: >> >>> >>> On Wed, Mar 3, 2021 at 5:49 PM Nikita Popov >>> wrote: >>> >>>> On Wed, Mar 3, 2021 at 4:28 PM Alexandru P=C4=83tr=C4=83nescu >>>> wrote: >>>> >>>>> Hi, >>>>> >>>>> This looks very nice and I'm interested in further steps where not >>>>> only new can be used :). >>>>> >>>>> The only thing I think it would be good to improve is to have a >>>>> deterministic order for running initialization. >>>>> Yes, this can be done at a later point, I guess. But maybe there is >>>>> already an order of initialization right now and people would start >>>>> replying on it and it would be good to mention it. >>>>> Or maybe I didn't understand what this refers to: "this is not >>>>> guaranteed behavior, and code should not rely on a specific point of >>>>> evaluation." >>>>> >>>> >>>> Which particular cases would you like to see specified? There are five >>>> cases that have clearly defined behavior, and that I could explicitly >>>> specify if desired: >>>> >>>> * Non-class constants: Are evaluated immediately when declared (i.e. >>>> when control flow reaches the declaration). >>>> * Attribute arguments: Are evaluated in the order of the arguments. >>>> * Parameter defaults: Are evaluated in the order of the parameters. >>>> * Non-static property defaults: Are evaluated in order of declaration= , >>>> with parent properties first. The constructor is run after defaults ar= e >>>> evaluated. >>>> * Static variables: Are evaluated immediately when declared (i.e. whe= n >>>> control flow reaches the declaration). >>>> >>>> And then there are the two problematic cases: Class constants and >>>> static properties. Currently, PHP evaluates these semi-lazily. All cla= ss >>>> constants and static properties are evaluated at the same time, on fir= st >>>> "use" of the class. I would consider this to be something of an >>>> implementation detail. That's what I meant by that sentence. >>>> >>>> Now, if we allow "new" expressions, then I could see an argument in >>>> favor of requiring class constant and static property initializers to = be >>>> evaluated eagerly, i.e. directly after the class has been declared. Th= is >>>> would be a (minor) backwards-compatibility break, because invalid >>>> constant/property declarations would error out immediately, even if th= ey >>>> don't get used. However, I do think that this would be the most predic= table >>>> behavior once potentially side-effecting expressions are involved (we >>>> already support side-effecting expressions right now, but less explici= tly). >>>> >>>> >>> Yes, this is what I was thinking about, to have a clear stated order of >>> initialization. >>> Yes, I agree that class constants and static properties should be >>> eagerly declared when class is declared. >>> >>> So the order would be: >>> - constants and static variables, when reaching the statement that does >>> the declaration >>> - class constants and static property, when class is declared, in order >>> of their declaration in the class >>> - instance property, when class is instantiated, in order of their >>> declaration in the class, before construct >>> - parameter defaults and attribute arguments defaults, when >>> function/method/attribute construct is called, in order of the declared >>> parameter/arguments. >>> >>> That sounds good to me. >>> Thanks! >>> Alex >>> >> >> I've updated the RFC (and implementation) to evaluate class constants an= d >> static properties at time of class declaration. As such, everything shou= ld >> have a well-defined evaluation order now. >> >> However, this also means that this RFC now also contains a >> backwards-compatibility break: Anything used inside class constant / sta= tic >> property initializers needs to actually be available at the time the cla= ss >> is declared. You can't first declare the class, then declare some >> additional constants it uses, and then use it. >> > > Another complication here is preloading. The current semantics of > evaluation on first use work well there, because class loading (during > preloading) is decoupled from evaluation (during request). Now, we can't > evaluate initializers during "opcache_compile_file" style preloading, so > we'd have to delay this to the start of the request. And then we'd have t= o > evaluate initializers for all preloaded classes, regardless of whether th= ey > will be used in this particular request or not. Also opens up the questio= n > of the order in which the classes should be evaluated. > > I initially liked the idea of evaluating everything at the time of class > declaration, but now get the impression that this causes more problems th= an > it solves, and we should go back to the previous lazy evaluation approach= . > Ultimately, my view here is that side-effecting constructors are a terrib= le > idea, and if you use them then you should also carefully manage those > side-effects yourself. > Just to throw it out there, another option would be to not support "new" for static properties and class constants, where the semantics are less clear cut than in all the other cases. For me personally the important use cases here are initializers for non-static properties and parameters, as well as attribute arguments, and those all have unambiguous semantics. Of course, there is also a lot of value in having consistent support. Regards, Nikita --0000000000003d697605bde35676--