Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120919 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23078 invoked from network); 19 Aug 2023 08:48:20 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Aug 2023 08:48:20 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3F10A18033A for ; Sat, 19 Aug 2023 01:48:19 -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-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 19 Aug 2023 01:48:18 -0700 (PDT) Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-565e54cb93aso1033321a12.3 for ; Sat, 19 Aug 2023 01:48:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692434897; x=1693039697; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=iP+SiudU0fna/C4AIOzRt+qi0vE5QljvH7kKVJn9/Rc=; b=VSvPpGBa2AO0MlHiFXfLGmtWXN1I9oyirsMAgmoTX703+h1/ucRuejyy14BTAP94RT 87JL9tYy9dc5OIQBBTuS6s1QSvORJEudtTD9qZkrUF2ZglxSbQ7SXEMdTdHliEWf9fOT GjrfhUAfczNoqFBpfLVxKDxfbKHu4pdUonA56myZ6JkiPwmnI+CtLwmQwm27G6FnE0jz ykLmb+gO8xoBjntZMt16Vwl8HlS2ezMlve042XNZKGLxtmTUFWbJJny87s/aprQUYScO 1M1uxqU2lsPT7KMx7lkOSx9CqoknGod1jXXYOGTOi95xUOcU0qwHOl/RwHzFJfUHrqZb sDDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692434897; x=1693039697; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iP+SiudU0fna/C4AIOzRt+qi0vE5QljvH7kKVJn9/Rc=; b=O0OG89nSVoK9gbneGzdhLLwZxEYzhc6PiczzDp2R7MwB4Go+0lRF1XhIlncxiMgXb1 v2fNZ9+mLIHlTLe6LAgjwB/1Nf5qkDN2olQCexdAObNnAdFHf+wFmC2pZJziBzOJayu0 0j4mskjDI2HlIORaHYhY08Y7hDSGD+vRkjak/ZPPvTgjn3xG4IWgofpnWF7xdPu68Swc ZWGxV37470OZY0/3+E+xyT7fYXBjUQ5aGYY1H6LGsnXxilfgoDiGrVUGqPgjFVD/k8FB xKOj9zBh7RoDV/E4Tg0+9IK6XubBg5+3w+r3bhoF2ZSYmCIiNQFQS1jC020VBrXHEvCv ZhbQ== X-Gm-Message-State: AOJu0YzP6L3Ec3QGAZutU5LCOEkFeyXdQiIXiaP1ID4tAMc77asRm1AU H1dOJcxjGoBgHeXttlc9UlPsueqDPObjaenwKu8FqzYVBe0= X-Google-Smtp-Source: AGHT+IErmKiLfOd32PxRmLJlD+SKvzO0giEqXS0Y4weqsM5wCFBxg6G8jlH8djL5/ttg2JjD2xTTH3h4x2d1bAKESiQ= X-Received: by 2002:a17:90a:4290:b0:263:e423:5939 with SMTP id p16-20020a17090a429000b00263e4235939mr982196pjg.28.1692434897614; Sat, 19 Aug 2023 01:48:17 -0700 (PDT) MIME-Version: 1.0 References: <64E00A5D.5010108@adviesenzo.nl> In-Reply-To: <64E00A5D.5010108@adviesenzo.nl> Date: Sat, 19 Aug 2023 10:48:06 +0200 Message-ID: To: Juliette Reinders Folmer Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Access property of object stored in a constant From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Juliette > > Since https://wiki.php.net/rfc/new_in_initializers we can store > > objects in global constants. However, we may not actually read or > > write to the properties of those objects without first fetching the > > constant into a local variable. > > > > const a = new stdClass; > > a->b = 42; // Fatal error: Cannot use temporary expression in write context > > $a = a; > > $a->b = 42; // Works fine > > > > This issue was reported twice, so it seems like this code is generally > > expected to work. > > https://github.com/php/php-src/issues/10497 > > https://github.com/php/php-src/issues/11781 > > > > I have created a patch here to add support for this syntax: > > https://github.com/php/php-src/pull/11788 > > I totally understand that people are trying to do this, but this still > very much feels like scope creep. > > IIRC the new in initializers feature was _intended_ only for enums > (which can't take properties). Now suddenly a "constant" would no longer > be constant... In which case, what's the point of declaring it as a > constant ? This patch doesn't change the mutability of objects in constants, as they already don't offer interior immutability. https://3v4l.org/s7rHE This is analogous to `const` in JavaScript or `readonly` properties in PHP, where we can't change the value of the variable (or const in this case), but we can modify the properties of the object it's pointing to. I believe the main motivation for `new` in constant expressions was to support nested attributes. Enums have their own mechanism for instantiating cases. Since it was decided to expand the support for `new` to global constants, I would expect it to work with other language constructs, unless there's a good reason for it not to. Ilija