Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117971 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 61390 invoked from network); 16 Jun 2022 20:58:40 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Jun 2022 20:58:40 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 56D5F1804AF for ; Thu, 16 Jun 2022 15:46:46 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f177.google.com (mail-il1-f177.google.com [209.85.166.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 16 Jun 2022 15:46:45 -0700 (PDT) Received: by mail-il1-f177.google.com with SMTP id a15so1883149ilq.12 for ; Thu, 16 Jun 2022 15:46:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Al8xaBtL+Axp2yAJjMVa7GrngLhrE1lcItQiQHeduCw=; b=TmcNMnog1EtzdomeaPX57tN61vWUGLDbk4Dr0v8qwk+KfrkOIkv0yV4w7415FZjsjO qPm6p/MwfHBwqasXuja8YSMfORNd+ObtI95qPGSFKFXe/weTzufBk9OBsqDCtUP62HAV Ia6uK3AnUPvXGww4u2KWK2eahhG1PubvDoOZIKqWK4Da1lS4vzfcCSCydZYUhw0LOc6r hKkvVwZl41rwk026RJVDTLW098UUFAIsUeyqaq1DeId0RGjsoOlPcS4SaoKwrEq9DhOU 1ll8Qi6JejWpGkBLYZgLt8QTKGyh45NI/M9VIrHvtTWnPDCnV8rM8hynmEQQeYvjVsTM blQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=Al8xaBtL+Axp2yAJjMVa7GrngLhrE1lcItQiQHeduCw=; b=kUfZxdEr8sU45mqsRrSYuQ84chtYVCXCS8T3ZDSKkvWhOJ0pgxLsqj/mkn55jft908 f8ewBieCdihVXql4NMY0piiPikdivJH6Lc8FvJBCcKCR3ApFq7u4m3W+q8s0LyHKrMoj dYuRAlABRmYnHHgweUYq8TgGand2duYAz5pvm80/R/6pAoyvTkGQ8C2dVYb/Hha1XF5x PXqbFiiL+uwUYXebnOf86SS5reBT832zKSlpWv6/bwEijnyrHL2bodpZP3SYL4tDRZMT VjYNHZGAUJSlghSQkkW9lKsJkx5ouDxqIYs3ksgVXV1r5MlbbYpatHY3XO/FA49S+jwa c/MQ== X-Gm-Message-State: AJIora/47ICLkYrrHhaj8fHjSk+uHxXgP53Vp5GGwz5SgQVeeefXGZ6k JJIPY1g2hL0cHl9NvBWFHdHGQTBTTjCHsde/GWL35UmsMPx8DQ== X-Google-Smtp-Source: AGRyM1udKhh+ZnMORwtiVokYfEbto3b9x7Eey6goTVY/jAWF0hW1d5EraCJGHUCKNzl/vcmdoRbOLDmLf1cUUN7LC/I= X-Received: by 2002:a92:c544:0:b0:2d3:9998:174a with SMTP id a4-20020a92c544000000b002d39998174amr4065167ilj.257.1655419604911; Thu, 16 Jun 2022 15:46:44 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 17 Jun 2022 00:46:33 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC][Under discussion] Fetch properties in const expressions From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Alexandru, hi Shinji > Hey, sorry to bump here on this topic. > > My assumption was that the use of `->` in initializers would bear the same limitations as using `new`. > That would mean it would not be supported for > - class constant initializers > - instance property initializers > - static property initializers > > For `new` clarifications are here: https://wiki.php.net/rfc/new_in_initializers#unsupported_positions > > My assumption is that `->` would only work for: > - static variable initializers > - global constant initializers > - parameter default values > - attribute arguments > > The examples shared by Shinji are for class constant initializers in class C and I'm guessing it would not work. > If that's the case, I think we should clarify this in the RFC as well. Thank you for bringing this to my attention. I have missed the case where, through constants, side-effects could be triggered in contexts where they shouldn't. class A { public function __get($name) {} } const A = new A(); class B { public $c = A->c; } I was under the incorrect impression that because (new A)->b is disallowed in property initializers __get could never be triggered in this context either but since the LHS of -> can be a constant containing another object this is indeed possible. The problem is described in the new in initializer RFC [1] you linked. There is currently no appropriate place where evaluation of the expression could happen without potential side-effects in places where we don't want them, like unserialize. Unfortunately, while thinking about this case I also discovered another issue. class A { public $b = 'b'; } const A = new A; class C { public $b = A->b; } var_dump(new C()); $a = A; $a->b = 'b2'; var_dump(new C()); Even without __get the property of an object referenced in the LHS of the -> operator can change. The engine evaluates the default property values once for the entire class and then copies the result whenever a new instance of that class gets created. That means further instances will use the out-dated property default value from when the first instance was created. Similarly, function default parameters get cached between calls. We could evaluate the constant expression for each object instantiation or function call but degrading performance for an edge case doesn't seem worth it. Since the primary motivation of this RFC is to allow fetching the name and value properties of enums I'm inclined to forbid using -> on anything but enums. That would circumvent the issue since enums can't contain user-defined properties nor the magic __get method, and name/value are readonly. I'll take a few days to think about the options, and will update the RFC and inform the mailing list about the decision I have made. Let me know if you have any more thoughts. Ilija [1] https://wiki.php.net/rfc/new_in_initializers#unsupported_positions