While all you say is true, blaming PHP’s arrays for Drupal’s code slips isn’t right. It’s far from PHP’s fault.
I disagree in that PHP as a platform set the stage. If PHP had a true array type to begin with the data in that function could have been bound to that type, dumping any unwanted text keys. The final responsibility does lie with the Drupal team, but to hold the environment the language provides as faultless isn’t correct either.
A lot of bugs and security exploits in PHP arise out of a variable unexpectedly changing datatypes. I have long argued that PHP should have a strict set of variables which must be declared to be used and when declared their datatype is locked down. Two subtypes of these variables would exist, tolerant and intolerant.
Tolerant variable have a locked datatype declared like so in proceedure
var integer $a = 1;
And like this in classes
protected integer $a = 1;
If $a is assigned a non-integer value, it immediately casts the value to integer. No notice or warning is thrown, this is what tolerants are for. Strict variables have a new keyword and are declared like so
strict integer $a = 1;
And in classes
public strict integer $a = 2;
If a non integer is assigned to a strict catchable fatal error is thrown. The whole point of strict variables is to die hard when such behavior occurs.
Changing the datatype requires unsetting the variable entirely, which is not possible for class members.
Yes, other languages have different names for the same structure and
yes, arrays as presented in PHP are generally called something else
elsewhere, but therein lies the catch - 0 simply isn’t equal to “0” in PHP
You know that, I know that, now the catch is explaining it to the beginners. Ease of access has always been a strong point for the language, and admittedly a weakness as well since loose datatyping has all sorts of pitfalls for the unwary. It’s still less intimidating than having to learn datatypes at the same time as learning variables.
As far as $a[0] vs. $a[“0”] I wish it would at least kick a warning. I see no reason other than sowing confusion to set up keys that way.
I agree with you on objects as keys - I find it confusing and unnecessary, too.
Having given this further thought I can think of a possible use case - keeping an object intact while using array_flip on the array that contains it. However, that case is a bit contrived and there should be better ways to accomplish this.