Let's be controversial: In modern #PHP, you should never type-hint an array.
Let's be controversial: In modern #PHP, you should never type-hint an array.
@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!
@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.