Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113378 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 67279 invoked from network); 4 Mar 2021 14:20:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Mar 2021 14:20:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 345751804B8 for ; Thu, 4 Mar 2021 06:11:04 -0800 (PST) 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-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (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 ; Thu, 4 Mar 2021 06:11:03 -0800 (PST) Received: by mail-lj1-f174.google.com with SMTP id y12so20576612ljj.12 for ; Thu, 04 Mar 2021 06:11:03 -0800 (PST) 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=YPOpmEpBh84xD23RODBPFnBXfAeijLtOGix6KUY6ADs=; b=ZoVBOQ//Unvl0MTUufuqdpE1undVF4stKOrpx6crO5MR3NqxS+H9IorCM0UYG+45ku i0bWhSDeIcnG313myYhWDTV7PSsjmv3IgMrV8pb/nPvov11pxbgeW+4/aMZ/S5Rxvrp/ F++BEq6eeQ6tdCD/hW3K6yR/b/vFgGFVqIx+aBSfbOFjdpbEeXwrkkzKZEd3KBwlG3gA NXNBSeOLnGBhGRkebIxlbunaXyoKyhEM2cNTN0tDIyfsM55e6+phtG0xC3hcwXcgqYTb Z6BQZYNRxBzRkepj8UX/ynoGu3uN1OBd1k8ZAwGySb5Ot106AUdOiD3RrMwIuIS5ESQU h/EA== 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=YPOpmEpBh84xD23RODBPFnBXfAeijLtOGix6KUY6ADs=; b=be5nhaTsazWBzGHDaOYlNPCoIbmYBCIvRK/wEOYxFBuk7Vo7zAWVewft5Wh0Vip2YZ ilIrWJjd/Yo77u1gJMWuh6wYgQ7STWRTB1kxWOk2CRqL3pRdyNLR+yStE59/Ta2ekORn BrqFn6i0YotEyqXD96yAdFyF2/mzZwuE96o683hFawWu9ZakfGFa86ZnpU10YDx4z9tp 45CTeDl/x1pbtSi2UJUzDbf/7JkzqTU0eK019gSVl6UfGINOt2PijVjXgYQcRUxrfzjR m+lCJ0nYJsZpp2ZPlmlSWkFoIZqCnGTtVP87HArnPzxY5ggv/MB3t3hlgOLVGlvnskFZ 8dcA== X-Gm-Message-State: AOAM533uLJrVWO7UnbNa0oAkDK5rgc0YmA/4RyUbiD/s1Yay49GArA63 B0+FhUDpyiHss98WNEhqMT9AouY3w+BX9fCnlmU= X-Google-Smtp-Source: ABdhPJzIXKqEwbob3VFa3x2z1SCS2MMPQqn2SHLOtYF1jckM0GfRmhVT5eA4G1dtw4q2OrRYYFpg/vBK5AhazpIQeUU= X-Received: by 2002:a2e:8503:: with SMTP id j3mr2417844lji.272.1614867061520; Thu, 04 Mar 2021 06:11:01 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 4 Mar 2021 15:10:45 +0100 Message-ID: To: =?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?= Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000008fbece05bcb6892f" Subject: Re: [PHP-DEV] [RFC] New in initializers From: nikita.ppv@gmail.com (Nikita Popov) --0000000000008fbece05bcb6892f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 are >> evaluated. >> * Static variables: Are evaluated immediately when declared (i.e. when >> 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 class consta= nts >> and static properties are evaluated at the same time, on first "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 favo= r >> of requiring class constant and static property initializers to be >> evaluated eagerly, i.e. directly after the class has been declared. This >> would be a (minor) backwards-compatibility break, because invalid >> constant/property declarations would error out immediately, even if they >> don't get used. However, I do think that this would be the most predicta= ble >> behavior once potentially side-effecting expressions are involved (we >> already support side-effecting expressions right now, but less explicitl= y). >> >> > 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 o= f > 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. > If we wanted to eagerly evaluate class constants and static properties when the class is declared, I wonder what the expected behavior would be if evaluation fails. If you have something like try { class A { const B =3D new Foo; // Let's assume this throws an Error. const C =3D 1; } } catch (Error) {} var_dump(A::C); then what would happen? Some initial thoughts would be: 1. If evaluation fails, don't declare the class. This is problematic because we may want to use the class while evaluating, e.g. for a "self::FOO" default. So I don't think registering the class only after evaluation is an option. We can also not un-register the class on failure (technically impossible). 2. If evaluation fails, set the remaining values to UNDEF. For static properties this will act like an uninitialized typed property, for constants we'd have to add an error condition for this ("Class constant not available due to initialization failure"). Also, an unrelated realization that I had is that our trait property compatibility check currently performs evaluation when checking whether two properties are the same -- but this seems problematic if the default value uses "new". Regards, Nikita --0000000000008fbece05bcb6892f--