Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129204 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 7E9251A00BC for ; Wed, 12 Nov 2025 09:54:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1762941245; bh=GQBixql5VbXYcXiMlRiQ+L5b7Vp17JRv+f1Mz8PXi9U=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=OvnD1QJYm2VHb3XUC9yIwOpflR6kgPx+FSzkX5YD/vrQbzlj7D004FWRZvRWYYPCF lo4xm0+E3mb6zwT5BN4ayUDpvQMesLBSPPKyG1vT6a1YhWsx/YodvKUJKpkomY8u9M gQVGkuceZNUvp9If3YxqnT3fNnvD0zTfYLIv+8zYL93D88GqF8SQCY0KCAXpUbinR6 QUuv/boZbWnYjqXi4+OscViMJ1PM1mn8nC1XNAk1wBg/mHMUXqGjUKuKwxOOdFMCEs RgsQAiLk1QuVD30lRpbEaUsdYxbphwAIpIQzk72OmLiOlTaT5k4tsKLKpo9SsGMJOR QFLz0lQTlyLGw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 14A0B180062 for ; Wed, 12 Nov 2025 09:54:04 +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,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (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 ; Wed, 12 Nov 2025 09:54:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1762941236; bh=t0xIqAFmh/vBSwnwSf51QpM7ccrgwxfed1u/kXf943s=; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:Content-Type:from:to:cc:subject:message-id; b=G2HPILkbSlo01PZUd/O1mb3V3zACb8a0AL56RN/UcCi2UoBI/L5/gYNkL/EQJCfUW O67E1byyqz5lhPF28URIQhSMcVQ5B2RK2YHBCzn9hm8D3iNvnmhgLfs5b1ATUYFEc2 dsOzd39bZWVDnVfdIuEnhAVxu1Y8WG/AchXo3GnXTE09Uiw2UU6lhwVVUEZJwVLoMN WNtnrF16NbgJWeBL4Fqznxerxp/Nt2Ue0eiwl68XKOUf5YBjruo3VQS3CEvOmyMHUT tUbqZlx5U1POq3iSz+k8BxLAWGNjrHak0YoU3MCU/7VhlHRNkSp5AKi6byG3F8sjEL DhFyYdVpUnwbg== Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Wed, 12 Nov 2025 10:53:55 +0100 To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Subject: Re: [PHP-DEV] [RFC][Discussion] use construct (Block Scoping) In-Reply-To: <84b9dc16-3eb3-4283-b015-3af29fc0e55d@rwec.co.uk> References: <1F3473C7-5D83-48D3-964E-A63D6F44D21E@rwec.co.uk> <4998b4c6-0474-4f0c-b63a-9909b8acfa96@bastelstu.be> <018421f64342a0d960589b4c8eea5cc5@bastelstu.be> <84b9dc16-3eb3-4283-b015-3af29fc0e55d@rwec.co.uk> Message-ID: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi Am 2025-11-11 23:43, schrieb Rowan Tommins [IMSoP]: >> That's fair. I'll look into adding more explicit comparisons to the >> RFC together with Seifeddine. My short answer here would be that >> JavaScript already had explicit scoping by means of `var` and thus >> folks are already used to needing to declare variables. Moving to >> `let` is just a smaller incremental change. This is different from PHP >> where all variables exist implicitly, which also means that semantics >> from other languages need to be adapted. > > > Technically, JavaScript variables don't have to be declared either; the > difference is that they are global by default, and since function-scope > is generally more useful, people are indeed very familiar with "var". You are correct, my remark was too simplified. To add to that: If you use `"use strict";` then the implicit fallback to global variables will not happen and an error will be emitted instead: "use strict"; function foo() { a = 1; } foo(); results in: "ReferenceError: a is not defined". > PHP does have optional keywords that declare a variable with specific > scope, just much less frequently used: "global" and "static". Regarding > the discussion on shadowing vs hoisting: > > - Both "global" and "static" can shadow local variables, and indeed > each other: https://3v4l.org/vPr2A > - Since the shadowing lasts until the end of the function, a shadowed > local variable is never re-instated, and will be de-allocated if it was > the only reference: https://3v4l.org/VBVkX > - Somewhat unusually, they do this as run-time statements, so you can > *conditionally* shadow variables: https://3v4l.org/fK9VJ > > Whether these are *good* semantics to copy for a block-scoped variable, > I'm not sure; but they are existing PHP semantics for a very similar > situation. Thank you. I just learned that `global` didn't need to be at the start of a function - which might perhaps be an indication that folks will already put the global at the top in practice, possibly because it is confusing otherwise. As a trivia knowledge I also learned that `global` supports variable variables: https://3v4l.org/oQLjP With regard to the RFC, I've adjusted the implementation (and RFC text) to make the following changes: - Neither `global`, nor `static` may be used from within the `use()` block. - `static` variables defined *before* the `use()` may not be used. `static` variables defined *after* are okay, since there is no ambiguity. - `global` variables may be used. This is consistent with `unset()` allowing to break the relationship with the global: https://3v4l.org/j2tRa. From what I understand, `global $foo;` is equivalent to `$foo = &$GLOBALS['foo'];`. >>>  I'm not aware of any language that requires a specific kind of block >>> in order to introduce a new scope, but that doesn't mean it's a bad >>> idea. The approaches I am aware of are: >> >> The closest comparison to “specific kind of block” might perhaps be >> older versions of C which require all variables to be declared - and >> for them to be declared at the top of the scope without any logic >> running in-between. > > > The big difference is the need for an extra level of indent (or, at > least, an extra pair of braces, which most people will probably assume > needs an extra level of indent). More often than not, there is an > existing block you want to scope to - a particular loop, or a > conditional branch, etc. Please note that the `use()` construct does not necessarily require braces. Thus both of the following would already work: use ($foo) if ($bar) { // } and if ($bar) use ($foo) { // } > I do see the advantage of forcing them to the start, though. Languages > in the Pascal family might be another comparison to explore - variables > are all declared in a separate block at the top of a function. > Off-hand, I'm not sure if any allow an arbitrary nested block just to > introduce additional variables. We'll look into more research in that direction. Best regards Tim Düsterhus PS: I'll be on vacation Thursday - Sunday, so please be prepared for some delay in replying :-)