Generics On Fri, 28 Feb 2025, 22:55 Alwin Garside, <al...@garsi.de> 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 >