Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126536 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 0C8281A00BC for ; Sat, 1 Mar 2025 07:38:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1740814536; bh=Co34AKcDCj5jc0IR61fXmQXAhoywozZNLt4vrNWWDZE=; h=Date:From:To:In-Reply-To:References:Subject:From; b=QFS/dNXf8zJQBlyImOxqKsIxn1/mCiOytpjPyDyhzDxYyolkTNppWxZGvTX8Ik8oQ 30Iq6DzCpUS2XVpyfJxp3mPBcwT9GwYEkz35UlmwGK3N2IZqCodmhtDx5vRs+mJr7K SFryK+QPEh1T9J6YyflFaZy/1oGcGgCvS8CEoyPYirl3TY98BY5R3uUI/GxQ2z5IjR 8U5UVToCkSvXcA8Vao7RZaKPbGFNn2BSkk8gHr4KsXEOhtWtnQEMGWy/kfR/6ZKQNv aybTf9SQMpM7xMySiZU1l5VNCKeuGxjwAGx7LP+tn21crItBeHWJo5xvS2R+Hy/Xkm /HzkECpwl4YEQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F2317180061 for ; Sat, 1 Mar 2025 07:35:34 +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.9 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-a2-smtp.messagingengine.com (fout-a2-smtp.messagingengine.com [103.168.172.145]) (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 ; Sat, 1 Mar 2025 07:35:34 +0000 (UTC) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id 5C2871381130 for ; Sat, 1 Mar 2025 02:38:11 -0500 (EST) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-12.internal (MEProxy); Sat, 01 Mar 2025 02:38:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=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=1740814691; x=1740901091; bh=Co34AKcDCj 5jc0IR61fXmQXAhoywozZNLt4vrNWWDZE=; b=cSAXoiywuPbFhtMPfDxP9x8Qjx te49HqrYF4pNE46THkNcLEs0+mQS36/b+g9NFfy4Kb+bZnHeIy4DswSCONlMepJ+ vOPpQaGK7wpy0kLKScmzWLdMznlcquVTtIz17E/S5d16cUiS7MiyUSdpEcNHF7b0 FA1v36UC7mtH71otBMOZVNlClMncoHecOvYQwmy2EmDqIrKaNzr14qvSZR/P6Qtf 5UtcXUFIn4U4oMS7G6R8D7751ogCBYcegW7dC0C1sYgAFktZoG+FnENG+t2x8mVB AMxqOevouGNXrPCdtxh6RTT+W7Ubixxcxu5impoVZhM+c8gDD0luMBRPLnLA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1740814691; x=1740901091; bh=Co34AKcDCj5jc0IR61fXmQXAhoywozZNLt4 vrNWWDZE=; b=Q17OkCB8CIo/FRRIRP33q1Zln/VzyFN01NLdAQID/XAyHS7gDl6 zHlXd4KTk5glfm5pDPBqZtHprrxyYJkZM6c3jgEHL2MHwpCLQdeOj0picuG1+KLN LQrHsenCvUg11ggaAjzPz3k74oCo3Zd23by1wUV5tjwP+uW8KfNo1F7R4+6/eptl F850iS/lbHOYq3ByYuDn7p5F5iFGCZ+oR/tLmNUhNJrsIuOSjQ29nAanfADuztCm dUzw/SIzQVwAtXKpQzxcge+H3QzW8A1nGfPhPnf4eodGM3Yj/KMD41ahjWdPXKss Kk+ji1+FbNTtcKOQbFL0N30Opy7ngop22lg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdelvdejudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepofggff fhvffkjghfufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdf uceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhepfeekge dvteegueegteevudduhfekfeffhfetffeuudegfeetheeuveduueevteelnecuffhomhgr ihhnpehhrggtkhhmugdrihhopdhphhhprdhnvghtnecuvehluhhsthgvrhfuihiivgeptd enucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtghouggvshdp nhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnth gvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id EF41A780068; Sat, 1 Mar 2025 02:38:10 -0500 (EST) 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: Sat, 01 Mar 2025 08:37:50 +0100 To: internals@lists.php.net Message-ID: <15994089-a8cb-4909-8f6c-b679b88efaab@app.fastmail.com> In-Reply-To: <81F87FFD-91EF-4FDF-A929-9BE1CA08AA1E@garsi.de> References: <81F87FFD-91EF-4FDF-A929-9BE1CA08AA1E@garsi.de> Subject: Re: [PHP-DEV] Vibe check: array shapes Content-Type: multipart/alternative; boundary=a0536a24b17d488f876bdcbd45aa5f45 From: rob@bottled.codes ("Rob Landers") --a0536a24b17d488f876bdcbd45aa5f45 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Fri, Feb 28, 2025, at 23:52, Alwin Garside wrote: > =EF=BB=BFHello, >=20 > Long time listener, first time caller here. >=20 > Last week I had a shower thought about how it would be neat if we coul= d declare array shapes in PHP, and use them to provide structural interf= aces for arrays. >=20 > After all, arrays are one of PHPs strongest features, and anyone who h= as ever tried to use arrays/maps in other languages after getting used t= o the versatility of PHP arrays will have to admit that nothing comes cl= ose to their flexibility and ease of use. >=20 > However, ironically, most of my friends, colleagues and fellow PHP dev= s usually tend to shun PHP arrays, which has led to the widespread use o= f Collection classes and the like. The usual argument is that arrays don= =E2=80=99t provide any way to declare their structure, and thus don=E2=80= =99t provide the ergonomics of autocompletion (even if you declare the a= rray shapes with docblock annotations IDE support is iffy at best) and d= on=E2=80=99t provide any form of runtime type checking. >=20 > To me, providing structural interfaces using shapes seems like a perfe= ct fit to the flexibility of arrays. I imagine you'd define them in a si= milar way to an interface, declaring and typing array elements rather th= an methods or property hooks. They'd provide a way to validate the conte= nts of an array at runtime, and offer static analyzers and IDEs a robust= way to do type inspections and provide autocompletions for arrays. >=20 > Of course my first assumption was that other people much smarter than = me probably already had this idea, so I started doing my due diligence a= nd discovered that a draft for a Shapes RFC was written by Kacper Donat = back in 2021 [1], but unfortunately seems to have since been abandoned. = This is a shame because it lines up about 90% with my ideas of what arra= y shapes should look like in PHP. >=20 > I'm unable to find any reference to this RFC on the PHP Wiki nor could= I find any threads discussing it in the PHP-Internals archive, so I ass= ume they simply never got around to completing it, or there was some dis= cussion elsewhere that prompted them to abandon the RFC. >=20 > Anyway, I would love to try and push this idea forward =E2=80=93 eithe= r by contacting Kacpar, or writing my own RFC =E2=80=93 and have a shot = at implementing a proof of concept, but first I would like to get a feel= for whether this proposal would find much footing here. >=20 > So please let me know how you feel about the idea of array shapes in P= HP, and perhaps read the draft written by Kacper Donat [1] for a much mo= re eloquent example of what I'm trying to propose here. >=20 > Thank you all very much in advance. >=20 > Kindest regards, > Alwin Garside >=20 > [1] https://hackmd.io/@kadet/php-rfc-shapes >=20 Hi Alwin, You may be interested in http://wiki.php.net/rfc/records, whose goal fro= m the beginning was a new type that behaved like arrays (value semantics= , copy on write) as well as the ability to attach behavior to them. I=E2=80=99ve since abandoned it due to the poor reception it got on the = list and decided to pursue inner classes instead. However, I spent nearl= y a year thinking through that RFC, so feel free to pick my brain via em= ail, or a call.=20 The first implementation didn=E2=80=99t use classes at all, instead were= implemented on arrays and enforced an array shape. This meant that reco= rds were a subtype of arrays (so you could pass a record as an argument = in lieu of an array). This worked pretty well, but feedback I got from c= olleagues challenged me to add the ability for behavior. So, I had to ab= andon that implementation. So, if you look at that RFC and remove all th= e behavior, you=E2=80=99re basically left with a record keyword and the = ability to specify types and props.=20 The biggest challenges with that implementation (and array shapes in gen= eral), happen to be around WHEN to do type checking and how to get a han= dle on references. Further, there=E2=80=99s quite a few parts of php tha= t reach into the implementation of arrays and do something different tha= n the canonical implementation (opcache, is a good example here) that ca= n end up breaking things in fun ways. Particularly if you somehow end up= with one of these in one of the magic global arrays. Anyway, best of luck to you and don=E2=80=99t hesitate to reach out! =E2=80=94 Rob --a0536a24b17d488f876bdcbd45aa5f45 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable

=
On Fri, Feb 28, 2025, at 23:52, Alwin Garside wrote:
<= /div>
=EF=BB=BFHello,=

Long time listener, first time caller here= .

Last week I had a shower thought about ho= w it would be neat if we could declare array shapes in PHP, and use them= to provide structural interfaces for arrays.

After all, arrays are one of PHPs strongest features, and anyone who = has ever tried to use arrays/maps in other languages after getting used = to the versatility of PHP arrays will have to admit that nothing comes c= lose to their flexibility and ease of use.

= However, ironically, most of my friends, colleagues and fellow PHP devs = usually tend to shun PHP arrays, which has led to the widespread use of = Collection classes and the like. The usual argument is that arrays don=E2= =80=99t provide any way to declare their structure, and thus don=E2=80=99= t provide the ergonomics of autocompletion (even if you declare the arra= y shapes with docblock annotations IDE support is iffy at best) and don=E2= =80=99t provide any form of runtime type checking.

To me, providing structural interfaces using shapes seems like a= perfect fit to the flexibility of arrays. I imagine you'd define them i= n a similar way to an interface, declaring and typing array elements rat= her than methods or property hooks. They'd provide a way to validate the= contents of an array at runtime, and offer static analyzers and IDEs a = robust way to do type inspections and provide autocompletions for arrays= .

Of course my first assumption was that ot= her people much smarter than me probably already had this idea, so I sta= rted doing my due diligence and discovered that a draft for a Shapes RFC= was written by Kacper Donat back in 2021 [1], but unfortunately seems t= o have since been abandoned. This is a shame because it lines up about 9= 0% with my ideas of what array shapes should look like in PHP.
=

I'm unable to find any reference to this RFC on the = PHP Wiki nor could I find any threads discussing it in the PHP-Internals= archive, so I assume they simply never got around to completing it, or = there was some discussion elsewhere that prompted them to abandon the RF= C.

Anyway, I would love to try and push thi= s idea forward =E2=80=93 either by contacting Kacpar, or writing my own = RFC =E2=80=93 and have a shot at implementing a proof of concept, but fi= rst I would like to get a feel for whether this proposal would find much= footing here.

So please let me know how yo= u feel about the idea of array shapes in PHP, and perhaps read the draft= written by Kacper Donat [1] for a much more eloquent example of what I'= m trying to propose here.

Thank you all ver= y much in advance.

Kindest regards,
Alwin Garside



Hi A= lwin,

You may be interested in http://wiki.php.net/rfc/records= , whose goal from the beginning was a new type that behaved like arrays = (value semantics, copy on write) as well as the ability to attach behavi= or to them.

I=E2=80=99ve since abandoned it= due to the poor reception it got on the list and decided to pursue inne= r classes instead. However, I spent nearly a year thinking through that = RFC, so feel free to pick my brain via email, or a call. 
=

The first implementation didn=E2=80=99t use classes = at all, instead were implemented on arrays and enforced an array shape. = This meant that records were a subtype of arrays (so you could pass a re= cord as an argument in lieu of an array). This worked pretty well, but f= eedback I got from colleagues challenged me to add the ability for behav= ior. So, I had to abandon that implementation. So, if you look at that R= FC and remove all the behavior, you=E2=80=99re basically left with a rec= ord keyword and the ability to specify types and props. 
<= div>
The biggest challenges with that implementation (and = array shapes in general), happen to be around WHEN to do type checking a= nd how to get a handle on references. Further, there=E2=80=99s quite a f= ew parts of php that reach into the implementation of arrays and do some= thing different than the canonical implementation (opcache, is a good ex= ample here) that can end up breaking things in fun ways. Particularly if= you somehow end up with one of these in one of the magic global arrays.=

Anyway, best of luck to you and don=E2=80=99= t hesitate to reach out!

=E2= =80=94 Rob
--a0536a24b17d488f876bdcbd45aa5f45--