Let's be controversial: In modern #PHP, you should never type-hint an array.

https://peakd.com/php/@crell/php-never-type-hint-on-arrays

PHP: Never type hint on arrays | PeakD

Let's be controversial: In modern PHP, you should never type-hint an array. Before you start throwing tomatoes, hear me... by crell

PeakD
@Crell I would be honest if I tell that I use data-array objects for quick prototyping because I'm too lazy to create a class when I don't know if my data object may actually represent 2 or more classes later on.
@Crell and afterwards there is sometimes no time budget to convert these arrays to classes, or gets overlooked during code quality checks or reviews
@anthonyvdg Sounds like several problems rolled up into one.

@Crell mostly in my case, I want to retrieve data, transform it into something, maybe do validation on it and then get the data out.

With quick prototyping I end up propping all that data into 1 array, with each step sometimes not knowing if my array should have a different shape (because I want to put "wires" between an object and where the data should go.

@anthonyvdg Deserialize it into a classed object using Crell/Serde, build the validation into that. Using either Serde's callbacks, or property hooks, or something like Symfony/Validator.

Your object completed? Great, it's valid. And now you have an object to use!

@Crell well, I used Magento in my last years ^^
@anthonyvdg Magento uses Composer, right? Everything I described is installable stand-alone via Composer. :-) (Maybe Magento should just ship some of them natively...)
@Crell yes, there is composer, though I rarely check for packages during development, though now you mention that ;-)
@Crell Actually, Magento has their own class that acts as a parameter bag and it is sort of a fundamental data type that is used for things like db-objects, http request objects, ... just to add a small note ;-)
@Crell (and oh, naming variables vs naming classes...)
@Crell ofc with refactoring the code with proper classes expands the logic easily 3x 4x, and ofc no reason not to do these changes when the opportunity is available.
@Crell (well maybe except with parameter bag-ish objects where the properties are dynamic)
@Crell Well, this certainly is an opinion. Sometimes you simply want an array of ints or strings or whatever. Feels pretty sane to use an array for that.

@chrastecky Array as a simple list, sure. But then you're going to most likely iterate it, and so you can type it iterable instead.

But yes, the real villain is arrays as pseudo-objects.

@Crell iterable is fine if you're going to iterate, but if you want to use one of the plethora of array functions, you have to either write your own which work with iterables, typehint it as an array or convert the iterable to an array.

Iterables are sadly not as usable out of the box as arrays are.

Edit: But yeah, arrays as object wannabes are the biggest problem.

@Crell I thought this seemed really familiar, and then I saw the date… I probably read this soon after you first posted this also.

Sorry, but then, as now, I'm not really interested in writing my own iterable list thingamabob when the language already gives me one. Plus, when working with someone else's code, if it returns an array, I know what it's doing; if it's some custom ThingamabobIterator I can probably guess but I'm going to have to stop what I'm doing and look up the code to be sure.

@Crell And I wouldn't want to force anyone using my code to learn how to use my own iterators either.

In short, can't we just get a tight little "List" in PHP already and let "arrays" do the dictionary stuff? Yeah, SplBlahBlah but something first class and "native" would be better and more discoverable.

@Albright I'd be supportive, but that always runs smack into the typed arrays / generics problem.
@Albright I have a bot that reposts my old blog posts at random event few days. Some are quite old, but IMO evergreen. :-)