Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129784 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 1D8971A00BC for ; Sun, 18 Jan 2026 18:33:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1768761241; bh=zC+gUE+ebGSbtHohwy0hayN3/OgUoOXMueqt72Gy7Xs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=EaS+th0o0AQZtHEr4yf0ayyKNdOjS+quFdZfBeOuKcowDMfl89WmO0Yu7mt7o4Bc4 Zdp1DobORAwDZ56gIudc0h/o3TV2PiJk4HePX2GjEde9EfgqEc7B7d9vNU6Of38Vq1 B37yEhU8cYLAajnewBpG8dQymr3oi29x3bSWNHk5vnrXr2h4Prt6NPa6ytH6r+WUMl yhXTAEUQM1d3WlwNRGIQif9jvlthVgXQkDqqNdDjOSy0NoWT1wevnHw18BOYejvqlN K0YtY+7BTLSJDWZWINZsVH+3bSrRQwu+r1g9KPRgbbej95pfJaSV8m3qselrw2wvIh SUSK57Z+5ysFQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 863D6180080 for ; Sun, 18 Jan 2026 18:33:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,HTML_MESSAGE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from forward502a.mail.yandex.net (forward502a.mail.yandex.net [178.154.239.82]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 18 Jan 2026 18:33:58 +0000 (UTC) Received: from mail-nwsmtp-smtp-production-main-84.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-84.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:1311:0:640:df31:0]) by forward502a.mail.yandex.net (Yandex) with ESMTPS id 75D6E81177 for ; Sun, 18 Jan 2026 21:33:51 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-84.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id nXdi1NaG4Cg0-Vvfw3b31; Sun, 18 Jan 2026 21:33:50 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=php.watch; s=mail; t=1768761230; bh=zC+gUE+ebGSbtHohwy0hayN3/OgUoOXMueqt72Gy7Xs=; h=To:Subject:Message-ID:References:Date:From:In-Reply-To:Cc; b=hdUMFWsLh32AnNzIKYjs3r/6n6xpFPmqY+SXgg6ljKuOF1crMVubLiGz2BCNShpwJ 179gCcRn3fIQJBmLi2z/1tkrCmVKL21Y8vocyXcWTap5daJAdXSSNqN3kSKq4/n//h c4sCYcaH9B5ple97BIQMFyuiFGPmK+XjPTTMRHGg= Authentication-Results: mail-nwsmtp-smtp-production-main-84.vla.yp-c.yandex.net; dkim=pass header.i=@php.watch Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-b8718187eb6so550590366b.2 for ; Sun, 18 Jan 2026 10:33:50 -0800 (PST) X-Gm-Message-State: AOJu0Yzce1sDS67Uxx0+aEMa1L/Xdz6fdP7fDj804jPeGM05lCtILZ/g UMCfcl+bKI7zeOwnxdbDwVMWNGovf6FGqHMFDP17TFOCsnbR0UKkP8P+13GjLnYjddXfsL7b00t 8qCwhOxuWg2G2OarhcVj+B6e28ECI+5c= X-Received: by 2002:a17:907:e118:b0:b87:b0ba:5d2c with SMTP id a640c23a62f3a-b87b0ba6ec2mr291532766b.56.1768761229530; Sun, 18 Jan 2026 10:33:49 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <1d5a3698-2796-4bc1-ac34-79b8dbc03e48@mabe.berlin> In-Reply-To: <1d5a3698-2796-4bc1-ac34-79b8dbc03e48@mabe.berlin> Date: Mon, 19 Jan 2026 01:33:24 +0700 X-Gmail-Original-Message-ID: X-Gm-Features: AZwV_QhH_n5wildAHi1lJVH0b3gt2G21hP7KBEyWGG8jrIJHWjlVz8VQ71i12AA Message-ID: Subject: Re: [PHP-DEV] Object support in class constants To: "Marc B." Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000c75b830648add05c" From: ayesh@php.watch (Ayesh Karunaratne) --000000000000c75b830648add05c Content-Type: text/plain; charset="UTF-8" > > Is there are a reason for this limitation especially since "new" > expression is valid for argument default values as well? > From the RFC https://wiki.php.net/rfc/new_in_initializers#unsupported_positions > New expressions continue to not be supported in (static and non-static) property initializers and class constant initializers. The reasons for this are twofold: > > For non-static property initializers, the initializer expression needs to be evaluated on each object creation. There are currently two places where this could happen: As part of object creation, and as part of the constructor call. Doing this as part of object creation can create issues for unserialization and any other process that is based on newInstanceWithoutConstructor() and does not want to implicitly execute potential side-effects. > > Performing the initialization by injecting code in the constructor avoids the issue, but requires that constructor to actually be called. In particular, this would necessitate generating constructors for classes that do not explicitly declare them, and the disciplined invocation of such constructors from potential child constructors. The third option would be to introduce an additional initialization phase between creation and construction. > > For static property initializers and class constant initializers a different evaluation order issue arises. Currently, these initializers are evaluated lazily the first time a class is used in a certain way (e.g. instantiated). Once initializers can contain potentially side-effecting expressions, it would be preferable to have a more well-defined evaluation order. However, the straightforward approach of evaluating initilizers when the class is declared would break certain existing code patterns. In particular, referencing a class that is declared later in the same file would no longer work. > > As such support in these contexts is delayed until such a time as a consensus on the preferred behavior can be reached. --000000000000c75b830648add05c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

>
> Is there are a reason for this limitation= especially since "new"
> expression is valid for argument = default values as well?
>

From the RFC https://wiki.php.ne= t/rfc/new_in_initializers#unsupported_positions

> New express= ions continue to not be supported in (static and non-static) property initi= alizers and class constant initializers. The reasons for this are twofold:<= br>>=C2=A0
> For non-static property initializers, the initializer= expression needs to be evaluated on each object creation. There are curren= tly two places where this could happen: As part of object creation, and as = part of the constructor call. Doing this as part of object creation can cre= ate issues for unserialization and any other process that is based on newIn= stanceWithoutConstructor() and does not want to implicitly execute potentia= l side-effects.
>=C2=A0
> Performing the initialization by inje= cting code in the constructor avoids the issue, but requires that construct= or to actually be called. In particular, this would necessitate generating = constructors for classes that do not explicitly declare them, and the disci= plined invocation of such constructors from potential child constructors. T= he third option would be to introduce an additional initialization phase be= tween creation and construction.
>=C2=A0
> For static property = initializers and class constant initializers a different evaluation order i= ssue arises. Currently, these initializers are evaluated lazily the first t= ime a class is used in a certain way (e.g. instantiated). Once initializers= can contain potentially side-effecting expressions, it would be preferable= to have a more well-defined evaluation order. However, the straightforward= approach of evaluating initilizers when the class is declared would break = certain existing code patterns. In particular, referencing a class that is = declared later in the same file would no longer work.
>
> = As such support in these contexts is delayed until such a time as a consens= us on the preferred behavior can be reached.

--000000000000c75b830648add05c--