Numeric and integer keys
<?php
$a = 1;
$b ->$a = 'foo';
var_export((array)$b);
var_export(array(1 => 'foo'));
?>Results:
array (
'1' => 'foo',
)array (
1 => 'foo',
)Observe the
'1'. The array element after casting can not be reached via the [] operator. (And no, "'1'" wont do the trick.)

Comments
It's casting the 1 to a
It's casting the 1 to a string because class variables cannot start with numbers, just like functions. Doing
$b->$acaused $a to be converted to a string. However, that is very WTF about not being able to retrieve the array element using []. At least you can still get it if $b is still an object and you can dovar_export($b->$a).Post new comment