Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:130162 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 CC5F61A00BC for ; Tue, 24 Feb 2026 20:27:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1771964835; bh=9Ul4OsyJMbZlnfjhJCMxrc5GtT+giW1XWF4VkOSfD0g=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=BPw6LBGhXvioiSJfDvfD2XhO3l9lxxoWTSygQkQe+g9lubynLAke1XoYjZ6PueBb6 V/8+ZWXMOxGKbSg60SjC8A2zpKeR8BdPY+Lt+86Vm5eIxtKjHPK27Vf/qV81fGITc7 lENIsRHiz5fHNyyg5AJoCebY6+OQGiWlLKRDdOToc4kATJq2blP81R3OTZtU8Ptt3G vvLOQao1ntmEtCkuY7SZN/0aDBM/3iWvGkxRnEFpe13VJ8/H8r9gHsP81TkDkBs2j1 35HaSr/htg5WxopEmVviIM95/xIYZSuNh3oMKiLSAwBXXV+ohWXd5Lp5FluStPhvgs gRBj/GQkd9PAA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 86E2C180080 for ; Tue, 24 Feb 2026 20:27:14 +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=1.9 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_50, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FORGED_GMAIL_RCVD,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 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 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 20:27:14 +0000 (UTC) Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-3870902760cso51167621fa.3 for ; Tue, 24 Feb 2026 12:27:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1771964828; cv=none; d=google.com; s=arc-20240605; b=JtOiG22ozjHqTKcpAUSTJbQMQ6WAv2GtLLtBbpxJ627AWMKDigHREBA7Ba6+QCO+o4 fmtN6pJMALPFzsbaN5uVUyiEcysV9CRKfPdk2tYsbXVuugGj63cHu5zmOj1mZD8t1HVZ iCH/GOKfnGcesuD952MPuperDPs4VQnf5rCYSvT80IWG2KOa+YBLh8vBuh2lvdAUNYB9 V9GBbgPvWbtRrAO8S7293jwPUFXWkd69AKMA2Wz1WxGl4SlnADjdBxrSrmTIkV5ssYnS vQ4h2Q82WDOmA4faA9i4dMcx+G/CTlUDWKyNGK/qEtY4BntyAgd0ONHFXuUTrER4Qash 1imw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=Uw1dbACASvkVf2KnpqwaydwRESHM1vALcc9/sQsMklo=; fh=PrQHppe/P+V/r9Ep2FUqtk4DMOb3OgTy7AMUjByVH64=; b=XnFZA+5hJaNy3DXjeJs36ZLpwaASOhBRjOlbU5ohnoj3kw4wLHtpQc0vzElCR9WW/n JMPqR4qzUggzrUsqvommlELYMm+kmerMtw7W2kwgOUrAfwkNrNIJa1M5eqFz+LiQ9D+P uH1HyEyic5Xq+efO731aj6zh71pVAXV2KIx+R0AZdg7n/PbH1CCp+YXHS90vw1ArIFGC pj8FQnGhUbpV91rWpwowET+wA3fS+gLAJpj6psnVQRNg3RVCW3XbppWslTL1UU+cyxT7 PtSDmfU3nNk0ZRWCXaLHHiCv4xMt5cBnvSqCOHxMAVsLvt/irQ0l2Mm4ZnUrkEyVxyqa H2tg==; darn=lists.php.net ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771964828; x=1772569628; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Uw1dbACASvkVf2KnpqwaydwRESHM1vALcc9/sQsMklo=; b=LkC83kNDwtWZRRuxlFEodDC6u3yhxNXP47WtTZx82JQEqq4epEzjFxh6Jg+6NoE4Qs XswKZDbMFESqddXdCsw0jUm6CnsB7D6TxUYzcjbDZFrF0vC02GlvTIIH+DlCNVLEkJj5 TLekeXeabN6Gho6voy9/JT9GtNtCDTpDfVfP+3AXPp59sSassLYhM609LGeW6YwJp4r5 kUfuYw6SC2hbZQPnnnFY4zJO7ZieuRuFAIbmopHymWBppqSNMkyrQEcmipQ6d9WHaQgu fzq5UJJYkvFTYF0GmzOeT7M7ySwfT9Q80CvalR7podtJTETL1VylxTSUAUSYESQTADAz Ei4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771964828; x=1772569628; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Uw1dbACASvkVf2KnpqwaydwRESHM1vALcc9/sQsMklo=; b=Fy4EC6yYd2WQnApARymjtVFvSjaOaFqaHGzgoc9rJ3gpBSP2Zmgqb47/8S5R321Fx9 /BiNy+bXDjE5ugGj3D75APTDwB3j//wCLzHdOpfgN7Qb08E6KsEMfIK7IIA4IDIFH1fC gNviA/hj2+X7uhgabFwEHQLMf/MS87eiitdlkf9wKckO7Yhi4BXzz0qHeGwxs2XlddrP 2BNV67lfCoJbMfk+uW9Ek1gHKAAFqVfXRi0iE2kLyy2mp21G6PvEYKxK5OS9R9vM9LCq 0yt2YVCIquaBVfQC0m5sBl0ZZSxie9L7i2lOJkNYH80ga/4xEMW8GkIO2s8oxm7qCOAy 9+Zg== X-Gm-Message-State: AOJu0YxW4bfCxNA7DABZ73TLDQaDVWUAvtRVuMjvDjsMAY1UlaGteKmB iMmJLLr6IjIoObldYmUSyS/LHwl1TDFyRR7CHRzwiYEpqebgkIaIPxNUOiSVETzDp5RZKb8fror DNSN8gvIpGdfFbeQiVkNabzAHarDH9AKAFEJr X-Gm-Gg: ATEYQzzo9750CQvz7MxF3jLPsI6yyJ/aQE0pQS2wqPvqDDe48a8A4Km0iJAmsr3BM1N 7OAor8sk7opHm3CcjLB8eTzMLjt12+JiIKffqhDPGJBdz8Ry41M1AJVkaohz8UxGygyOTIZ4Elw ey7qY41p9ToaK6nc5GFBkgsXXHuGphtehM8EpCwmf2mPvPNUa3j4BAAdiFInoauoN8gKLRESaZ4 6Xgzw330PoykcM+eALKRO/B46pUwmrk0X1oWIfDDtseDJtPsZRUbPMA3B/bSzVRYlawsPM1Errf PpYgAcg= X-Received: by 2002:a2e:b8cf:0:b0:383:3e5:2379 with SMTP id 38308e7fff4ca-389a5bd9cc1mr46743441fa.27.1771964827462; Tue, 24 Feb 2026 12:27:07 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <7FA75C32-BC65-487B-BE44-272FEED42F04@wcflabs.de> In-Reply-To: <7FA75C32-BC65-487B-BE44-272FEED42F04@wcflabs.de> Date: Tue, 24 Feb 2026 20:26:56 +0000 X-Gm-Features: AaiRm536qP0kR7pfQ70SJDU0XN0Q_rK0cJJQrNhKJruAueNPyEDkZXWUT99cdCw Message-ID: Subject: Re: [PHP-DEV] [RFC] Readonly Variables To: =?UTF-8?Q?Joshua_R=C3=BCsweg?= Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: tekiela246@gmail.com (Kamil Tekiela) Thanks for the replies. > > 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 iterati= on, PHP would attempt to re-assign an already initialized readonly variable= , which results in a fatal error. A "single declaration" therefore means th= at the readonly variable can only be initialized once throughout the entire= execution of the loop, not once per iteration. So does that mean that it can only be used within a loop if that loop only has a single iteration or that the assignment is guarded by some condition? $var =3D null; while(true) { if ($var =3D=3D=3D null) { readonly $var =3D 42; } } > > 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 sc= ope 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. Re= adonly variables follow the same scoping rules as regular variables, meanin= g they are not automatically available inside functions, just as regular va= riables are not. > If you meant something else entirely, please feel free to clarify and I w= ill be happy to address it. I meant using $GLOBALS. readonly $var =3D 42; function foo() { $GLOBALS['foo'] =3D 'bar'; } foo(); var_dump($foo); > > 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 wit= h the readonly keyword: > > ``` > readonly $foo =3D "bar=E2=80=9D; > ``` > > You are right that PHP has not had a formal concept of variable declarati= on until now. The readonly keyword in this context is purely a modifier on = a standard assignment statement, signaling that the variable cannot be re-a= ssigned after its initial value has been set. Any valid assignment expressi= on can be used as the initial value, including object instantiation, functi= on calls, or scalar values. Right, but if PHP users have never thought about variable declaration, then it might be confusing for them to understand when it is legal to use this keyword. For example, you said that array destructuring is not supported but for me it's a regular variable assignment. It seems that assignment within a loop is also not allowed, but that doesn't seem to be clearly specified in the RFC. while(readonly $arr =3D $pdoStmt->fetch()) {} // Is this allowed assuming it only iterates once? foreach($arr as readonly $element) {} // This is forbidden, correct? for(readonly $foo; ; ) {} // Is this allowed? if(readonly $val =3D foobar()) {} // This is a regular variable assignment, is this allowed? What about array vivification? readonly $arr[] =3D 42; Or a declaration with a warning: readonly $i++; What about a slightly more complex variable assignment? $arr =3D [readonly $var =3D 42]; // or readonly $var1 =3D $var2 =3D 42; // or foobar(readonly $baz =3D 42); // or $foo =3D 'bar'; readonly $$foo =3D 42; Regards, Kamil