Ideally the maintainers of PHP would rename it and deprecate the use of `array()` over a long period of time.
Well, there's no reason a language could not initialize an empty array to some initial capacity, either on declaration or when you add an indexed element.
$arr = []
doesn't appear to be initializing to any capacity. So that after:
$arr[2] = "two"
I would expect:
count($arr) === 1
And honestly, sure. A language could be designed to pad the array with empty values if you specify an index greater than the upper bound (or lesser than the lower bound). I believe a sibling mentioned matlab did something like that.
But is that really better behavior or just different behavior? I don't really see an important difference. My point was that the criticism was fairly weak. PHP, indeed, has a very useful abstraction in its concept of "array". Most of the hate should be directed at the name, not the construct.
$id = 12835151;
$arr[$id] = Get_thing_with_id( $id );Obviously, I'd agree that a map/associative array/dict/etc is a better choice here; I'm just annoyed about the name.
You start with an _empty_ array, then (somehow) set the third element of that array? That makes very little sense.
Had you _initialized_ your example as an array with a length of 3, your desired behavior would, indeed, manifest.