Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125431 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 qa.php.net (Postfix) with ESMTPS id F30891A00BD for ; Wed, 4 Sep 2024 21:41:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725486204; bh=KActs9ryGezQGoejGhMM+wCHLLZkBu7IwFUFEcbuJGE=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=L4AicK7nKHQ2uBdUCdIT25325WIOkFurH53lJ9O4kT+d8KfroIHcT76RVcSlsg8yj XG2CLMbICJkj/XUea9nI0GVbeBr5DAyhBy8NC2uOjXHE+ySqBnVPQyr8Gkz/A27fSz gMUFoti6LayKMwW+2lk+Pw6a6Ef1XSOjO4k2asoZDQytW1uAbJwKjzJk2pXNC4Sk+j 43IU9HQk6mnb3t3e2+W8c3fpBD97IWV+fph8tookq6pU4ibLxPVgoCOjLHrnJbr7LL Qo3rM+CLaIy14GMFThbgTyIk8MXz6a9276oHBLrELVFffmZ7AZ3YTGbO6Fd31t629u 8gF0rzmC8LGAQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A34E4180069 for ; Wed, 4 Sep 2024 21:43:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh6-smtp.messagingengine.com (fhigh6-smtp.messagingengine.com [103.168.172.157]) (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, 4 Sep 2024 21:43:23 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfhigh.phl.internal (Postfix) with ESMTP id B04811140366; Wed, 4 Sep 2024 17:41:24 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-03.internal (MEProxy); Wed, 04 Sep 2024 17:41:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1725486084; x= 1725572484; bh=j4FPuZdve656Ex17/KW4eHbk/dl+to5LYAjPZQwlRdo=; b=X 6op5pG+7oGwMR7EcYxX6bgDrWtgPCcHTciQPxOrzpyKcJkRUys26E+x5ALMCqBTG /HQI1E/bbHwsjs6JmAQg26ElFMpJQfg7RDEE6zGVgAqBNx9eyJZqmkV6mlPRAbVM /DX0zmV4hA0s+h7Y7jYzRO5gKLFtlt9cRpZhtEuVrWjb4zZ5Vv5t+59Bspd3XIPg Uu5WxGGGvbzFr2eHYB1iBME5zY+1Ar6M0f7IMhkzE+s7q+IORxt3GYEApEIU+CTr 3vdt/bH7dP5MSLyr0+T43AEY+qJLYLKXBEjee8STWlgZD/wvsu4fOEjRNFfr9OOn QP+7uBSbWcYeLHLmv6sHg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1725486084; x=1725572484; bh=j4FPuZdve656Ex17/KW4eHbk/dl+ to5LYAjPZQwlRdo=; b=E1/pNWNYjZJP+BIghCafNehznn0GrVQcCzZrx9Ytip23 4FpfBOvukCKcBhhQNO/jtFd6VI+tzqnb8bjD3WWE0329epWEISrzeAAXddNIBSMV Bglqz/cQwoigj8hYTXYGUnRyvGcL3ss8olGlTlkHckhsJDDpSy8qRgahc8qpqnTQ R9LjqYLoEOtnSTPUNN8PQ3VwCQ+Cau/Q2R+PXZuULQLJNuv2c6rEk8MOgODSA1kQ aeylXn0HagAyhTTtV94ol2hje9KeGYhtKpuuUbc3R+KvQ3qK7m3CtY72O6UBqyIi 8c4M6g+8Pmf6SC8ogBgFBJWuYCI3BAAGDvfJCjnQTw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudehkedgtdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepofggfffhvfevkfgjfhfutgesrgdtreerredtjeen ucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtoh guvghsqeenucggtffrrghtthgvrhhnpeeiueethedvvdefjefhgfeiheelheehtdfhfeek jefflefgvedvkeduteejjedttdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgtphht thhopedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhsse hlihhsthhsrdhphhhprdhnvghtpdhrtghpthhtohepjhgsrghffhhorhguseiiohhrthdr nhgvth X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 4790B780067; Wed, 4 Sep 2024 17:41:24 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Wed, 04 Sep 2024 23:41:04 +0200 To: "John Bafford" Cc: internals@lists.php.net Message-ID: <94510a5e-ae23-4118-ac55-2e90c911e7da@app.fastmail.com> In-Reply-To: <7A98532B-7465-4FC5-B7A9-993E7D430EE1@zort.net> References: <27c3909f-05f4-4256-a447-10e8d8760fff@app.fastmail.com> <420bb5cb-5fca-4f2e-8c68-0ca327cd3392@app.fastmail.com> <7A98532B-7465-4FC5-B7A9-993E7D430EE1@zort.net> Subject: Re: [PHP-DEV] Local constants Content-Type: multipart/alternative; boundary=c0f6e3fbc97d4729865b8dbbc1dd8a2a From: rob@bottled.codes ("Rob Landers") --c0f6e3fbc97d4729865b8dbbc1dd8a2a Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Sep 4, 2024, at 23:11, John Bafford wrote: >=20 > > On Sep 4, 2024, at 16:45, Rob Landers wrote: > >=20 > > I think, conceptually, this makes sense, but maybe I'm the only one = who writes > >=20 > > $arr =3D doSomething(); > > $arr =3D array_map(fn($x) =3D> $x->prop, $arr); > > $arr =3D array_filter($arr, $filterFunc); > >=20 > > instead of > >=20 > > $arr =3D array_filter(array_map(fn($x) =3D> $x->prop, doSomething())= , $filterFunc); >=20 > IMO, that's a failing of PHP not supporting object syntax (and reasona= ble api) for non-object values. In other languages, I can write code suc= h as: >=20 > let arr =3D doSomething() > .map { $0.prop } > .filter(filterFunc) >=20 > Which is more readable than both PHP versions. I think that sidesteps the reality we are currently in and is orthogonal= to the actual problem I perceive. To be more clear, I think with the cu= rrent APIs in PHP, having local constants would incentivize people to wr= ite less readable code for the reasons you mentioned. Could the APIs be = improved? Probably, if not certainly, but that is not what is on the tab= le.=20 >=20 > > And I feel like having constant variables would result in more of th= e latter, which I feel is more unreadable. Though I do note that my opin= ion of what is readable might be different from other people's. > >=20 > > That being said, I would much rather see block-scoped variables than= local variables via let or var or something: > >=20 > > var $aNumber =3D 12; > >=20 > > foreach($arr as var $item) { > > echo $item; // item exists here > > } > >=20 > > echo $item; // item doesn't exist here > >=20 > > PHP is simply too verbose to really benefit from local constants, bu= t would benefit from block-scope far more. For example, with local const= ants, you couldn't write that foreach because that variable exists in th= e scope of the function and can only be defined once. > >=20 > > =E2=80=94 Rob >=20 > I'd suggest that the foreach could be written with either of let/var t= o both locally scope $item, which would explicitly disallow/allow $item = to be changed while in the loop. Maybe, but I only mention that block-scope would be more valuable than l= ocal constants. That they work well together is also interesting. >=20 > Another benefit of local constants is preventing accidentally reusing = a prior variable. For example, if I set a temporary variable to somethin= g, and then later in the function, reuse that same name for a new tempor= ary but don't set the value on all flow paths, then the value from earli= er in the function leaks through, whereas either using a constant, or a = second explicit variable definition would have exposed and/or prevented = the issue. >=20 > -John This is a problem that I have run into in only a handful of times in my = entire career, across all languages, lots of times in JavaScript files o= f yesteryear when we had to deal with 10k loc handrolled files. I=E2=80=99= ve seen this happen maybe 2-3 times in php (where it has been a bug), an= d a couple of times in C. I don=E2=80=99t think I=E2=80=99ve ever run in= to it in C#, Scala, or Python. Maybe I=E2=80=99m just lucky, but I don=E2= =80=99t feel like this is a valid reason for the feature. Const was added to JavaScript (according to my cobwebbed memories) to in= troduce block scoping and optimizations around that. In PHP, we also hav= e function scope, like JavaScript, but we also have lexical scope as wel= l. Javascript did not have that at the time; in other words, the followi= ng would have output "world": function test() { console.log(hello) var hello =3D 'world' } test(); Today, it doesn't, and neither would php. The problems that const/let se= t out to solve in Javascript do not apply here, IMHO. =E2=80=94 Rob --c0f6e3fbc97d4729865b8dbbc1dd8a2a Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable