Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:130154 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 EFF841A00BC for ; Tue, 24 Feb 2026 18:52:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1771959163; bh=JPq9TjcCEcFTgJYRY7Gv8txClTof86JkQ6piYe425qs=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=TYa9SVZGLacpluXzyqSMGRfgUQSXbMUFY17DYoaHd89vTdB2tXD0ncct0bBBfAugO FR/ujj0VKXyK/Uw4E/VY69wnnO9Yf26YOvqU/y3p2Tf1FH2OpGChsn8QImUk1f7qxT cLOj561LsP5n2fgnu3QeJp757JASwDAh9hwkz1f0oTfARRdML/aBWCVi5UBVXuh9FT 63bH0CdkchlqPaB7Hd8O+417U2B+ImAlJbttBiVTvgq6hzQESO1xMWyINGj8mE/o5T P+hkABp2Jzz1R/0rTyiJ9w+H3KjuC8LwY1O89pNIAtp44WNJureqOZxg7rnMeQsGww y5DV5JSjFES5w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 33F751801DA for ; Tue, 24 Feb 2026 18:52:39 +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,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,T_SPF_TEMPERROR autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 ; Tue, 24 Feb 2026 18:52:28 +0000 (UTC) Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4836f4cbe0bso44741785e9.3 for ; Tue, 24 Feb 2026 10:52:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wcflabs.de; s=google; t=1771959143; x=1772563943; darn=lists.php.net; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=JPq9TjcCEcFTgJYRY7Gv8txClTof86JkQ6piYe425qs=; b=grUjLSYxpfEIX9J+EH8CeeaOXIl+ERAnDnTwYPNwNwIL2sJr9ueshRo5qL6+/CJxyh D7FWzIkxvI1p71SvPKZrTslitBpBVMWQ9q0XBVhSqVfoRJfzCXfKLvb83rU1039vc5WW GpaPHNOQcWg+v5ScedrCe3sxTfssVlDnHkBLmZgDY5KWcLU9zvXu53bG30vuQrQXC4HB FIVHbc0Xjvmy5nCj7IbPV5w3UZ6N/+pdmXh4DuIf6CnYWVid3tofWtbdjCiu3S4Jo6sU 2M2pMJywepKYAv3e7SOF7eTzX6SfGQC8jEdIPB3cAJqSWsKRQVLoRyR2irhcQ1MPMk32 1RAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771959143; x=1772563943; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=JPq9TjcCEcFTgJYRY7Gv8txClTof86JkQ6piYe425qs=; b=Voeaxyz4Fzb5mck12Rldp2jq/IU37hZEAzgjUY7kJtoECZbEMJrAIP72ib2/p9Veud 33sE283rC/FHbdskliWEYGUdqMCZtjrAwc+/DogESVD1ckB6TrXbVh+bRKeo18z3QJk4 +XYru+1h7aRS18OzFSlThu1k0Sn4ZYjV53vGBd8ZIv2vWSc3CwyRPsp5lSjoOYfhqYh8 DScXIxwQ14oFPdmhlg64+7UlyPy+/zKuXa6i/mq4G7J+qr/Va4cq6OcsWI6kItwgE+uE XkkrSXqEj/3tkpJHn7ob2oRypmLM/hsp/yv+dBJ/w1mBhEchiH+AXDYgWfvAZRX/ZQMr Fzsg== X-Gm-Message-State: AOJu0Yx5RHodQnU2FQCqdb1oP61hMPcE414AwXDIbqs/sLVPwyaSiBvR +MHuHbjotsKjCmdiHja7X73BY/h/Tn8FBc+x7zdgpB9fBLVBmFGpa4m5sKoJpkb4sns= X-Gm-Gg: AZuq6aIPxZ8hPrvkenvygnAWc94w+seczsfYDKQN3jI7N+nweM6TGKPiJQmlY8b+pRr qQ7HPbzb6nPmZ3nicM1/sljQk7822D8zhxb5FkceEoW4DxwHHeznFfVO00ImmSo6w5iW80SfCTF PvZcz7I+Afnng4uAPbOR3RY6AjBnv7q5utEjC7sUOgm6TW6V6H51cWNec1LT/reXaSqsOpXsoFp znzng0EZYuHgZ4jw4lEPrOtL/eKocnL5gw6bD2RZt+E/nYFrlZDFNnBJJBxC56lgi2yNIK9AsXI Ug23lD+EJ8MsmrLW017WFokU7htboXW0ckbeRGT0CrhDQMyc2S77hklXU/NciB1SBEVPj8Rc5rs ptuATNELyAouXsKY8WSA32tPIzpLMKjqX0Q0Ru5qhU7WkHVADwqjBoZxYI2nDKRON+bmVbnWvHK rvMbiXLm7qQjrZEhAJLe37ESwU+YhK+MY+ZY2Rf97I+h0u6I45dYK8FT4LV/L5WNrnIjeBBxBkM IR/qJ5LhcOqtFdFdsdWH+qIIuexG0YCxBN24t8= X-Received: by 2002:a05:600c:5486:b0:483:709e:f239 with SMTP id 5b1f17b1804b1-483a95dea69mr200544605e9.22.1771959142542; Tue, 24 Feb 2026 10:52:22 -0800 (PST) Received: from smtpclient.apple (p200300e91f342b589d745db7d00ff1de.dip0.t-ipconnect.de. [2003:e9:1f34:2b58:9d74:5db7:d00f:f1de]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-483bd7031f3sm16655185e9.6.2026.02.24.10.52.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Feb 2026 10:52:22 -0800 (PST) Content-Type: text/plain; charset=utf-8 Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.300.41.1.7\)) Subject: Re: [PHP-DEV] [RFC] Readonly Variables In-Reply-To: Date: Tue, 24 Feb 2026 19:52:11 +0100 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <7FA75C32-BC65-487B-BE44-272FEED42F04@wcflabs.de> References: To: Kamil Tekiela X-Mailer: Apple Mail (2.3864.300.41.1.7) From: josh@wcflabs.de (=?utf-8?Q?Joshua_R=C3=BCsweg?=) Hello Kamil,=20 > On 23. Feb 2026, at 22:58, Kamil Tekiela wrote: >=20 > 1. Why the use of readonly keyword? Why not const or some new keyword, > e.g. locked. I spent a long time thinking about the right keyword. In an early stage = of development I chose `const`, as it is already used in this way in = JavaScript, for example. However, the further I developed the feature = and the more feedback I gathered from others, the more I gravitated = towards `readonly`. The `readonly` keyword is already used in PHP for = class properties and serves exactly the same purpose: declaring = variables as immutable. Reusing `readonly` for variables is therefore = consistent with existing PHP semantics. The `const` keyword, on the = other hand, has fundamentally different semantics in PHP. A `const` = declaration at the top level defines a globally accessible constant, and = inside a class it defines a class constant. This is quite different from = a locally scoped immutable variable, and using const for this feature = would likely cause confusion for PHP developers who are already familiar = with its existing meaning. Introducing an entirely new keyword for this = purpose, such as `locked`, would in my opinion be counterproductive, as = it could be unintuitive for developers who are already familiar with = `readonly` and its meaning in PHP. > 2. Why does unset not remove the variable completely? If you want a > feature to only unlock the variable then it probably needs a new > keyword, e.g. unlock Thank you for pointing that out, as I apparently expressed myself = ambiguously in the RFC. `unset()` behaves exactly as it does for regular = variables: it removes the variable entirely. The only additional effect = is that the `readonly` flag is cleared along with it, meaning the = variable name can subsequently be reused with a new assignment, without = the `readonly` flag. I have updated the RFC to make this behavior more = explicit. Regarding the idea of a dedicated unlock mechanism: I don't think this = is necessary, and I honestly don't see a practical use case for it. If a = variable needs to be modified after its initial assignment, it simply = should not be declared as readonly in the first place. > 3. What exactly does this mean: "Readonly variables inside loops are > permitted but limited to a single declaration"? What does a single > declaration look like? A readonly variable declared inside a loop is exposed to the outer = scope, just like any other variable in PHP. This means that on the = second iteration, PHP would attempt to re-assign an already initialized = readonly variable, which results in a fatal error. A "single = declaration" therefore means that the readonly variable can only be = initialized once throughout the entire execution of the loop, not once = per iteration. I hope this clarifies the behavior. Please let me know if you have any = further questions. > 4. How will this impact optimizer and JIT? Thank you for raising this point. I have not yet investigated the impact = on the optimizer and JIT, as the current implementation is an initial = proof of concept intended to gather feedback from the community. I will = definitely look into this before the voting phase and investigate = whether and how readonly variables can be leveraged for potential = optimizations. I will report back with my findings. > 5. You said that readonly variables cannot be used with the global > keyword. But what about readonly variables in global scope? Will they > still behave like a typical global variable? I am not entirely sure what you mean by "typical global variable = behavior", so I will try to address what I believe you are asking. If your question is whether a readonly variable declared in the global = scope can be imported into a function via the `global` statement, the = answer is no. Importing a readonly variable via `global` is forbidden = and results in an error. If you are asking whether readonly variables declared in the global = scope are automatically accessible in function scopes, the answer is = also no. Readonly variables follow the same scoping rules as regular = variables, meaning they are not automatically available inside = functions, just as regular variables are not. If you meant something else entirely, please feel free to clarify and I = will be happy to address it. > 6. What exactly counts as a variable declaration? When is it a valid > syntax to use this keyword? I don't think PHP had the concept of > variable declaration until now. By "declaration" I simply mean a regular variable assignment prefixed = with the readonly keyword: ``` 7. What about compound variables such as arrays and objects? The behavior differs depending on the type of value assigned to a = readonly variable. For arrays, the entire array is immutable. This means that neither the = array itself nor its elements can be modified after the initial = assignment: ``` value =3D "changed"; // Valid: modifying the object's internal = state is allowed $obj =3D new stdClass(); // Error: Cannot re-assign readonly variable ``` I have added examples for both cases to the RFC to make this behavior = explicit. > I would prefer starting with readonly parameters as I feel that would > bring the most value. I find the idea of readonly parameters interesting and have already = given it some thought. However, it is out of scope for this RFC, as I = wanted to keep the initial proposal focused and manageable. I have = already mentioned it in the Future Scope section of the RFC as a = potential natural extension that could be addressed in a separate RFC in = the future. > I think it would also be worthwhile to investigate a simpler syntax > for define(). It is worth noting that PHP already provides two ways to define global = constants: `define()` and the `const` keyword. Both serve a different = purpose than readonly variables, as they are globally accessible and not = scoped to a local or functional context. See: https://3v4l.org/1r6gk Cheers,=20 Joshua R=C3=BCsweg