On Fri, Feb 28, 2025, at 23:52, Alwin Garside wrote: > Hello, > > Long time listener, first time caller here. > > Last week I had a shower thought about how 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 close 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’t > provide any way to declare their structure, and thus don’t provide the > ergonomics of autocompletion (even if you declare the array shapes with > docblock annotations IDE support is iffy at best) and don’t 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 in a similar way to > an interface, declaring and typing array elements rather 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 other people much smarter than me > probably already had this idea, so I started 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 to have since been abandoned. This is a > shame because it lines up about 90% 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 RFC. > > Anyway, I would love to try and push this idea forward – either by contacting > Kacpar, or writing my own RFC – 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. > > So please let me know how you 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 very much in advance. > > Kindest regards, > Alwin Garside > > [1] https://hackmd.io/@kadet/php-rfc-shapes >
Hi Alwin, 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 behavior to them. I’ve since abandoned it due to the poor reception it got on the list and decided to pursue inner 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’t 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 record as an argument in lieu of an array). This worked pretty well, but feedback I got from colleagues challenged me to add the ability for behavior. So, I had to abandon that implementation. So, if you look at that RFC and remove all the behavior, you’re basically left with a record keyword and the ability to specify types and props. The biggest challenges with that implementation (and array shapes in general), happen to be around WHEN to do type checking and how to get a handle on references. Further, there’s quite a few parts of php that reach into the implementation of arrays and do something different than the canonical implementation (opcache, is a good example 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’t hesitate to reach out! — Rob