I may have just ran into one of those "wtf PHP?" moments.
According to the PHP documentation [Class member variables] are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration.
I would assume this means that properties must adhere to the same naming conventions as variables. Namely, it must not start with an integer. The following code, does indeed cause a parse error:
class Foo {
public $1st_property;
}
The documentation also states when casting arrays to an object: Arrays convert to an object with properties named by keys, and corresponding values.
So I tried
$a['1st_key'] = "Hello, World!";
$o = (object)$a;
print_r($o);
And 1st_key
is indeed a property
stdClass Object ( [1st_key] => Hello, World! )
Point being: The property name begins with a number, which is not a valid variable name (of course, we can access that property with $o->{'1st_key'}
). But, howcome when an array is cast to an object, invalid variable names can become property names?
extract
drops invalid names (or prefixes them, as you choose).