0

Should this even be possible?

$array = ['1invalidName'];
$object = new \stdClass();
$object->{$array[0]} = 200;

var_dump($object);

$object->1invalidName is a syntax error, only way to retrieve the value is through $object->{$array[0]} again.

Rikard
  • 83
  • 1
  • 9
  • 1
    Because variable can't start with a number. – AbraCadaver Feb 22 '17 at 22:00
  • But shouldn't that apply to object attributes too? – Rikard Feb 22 '17 at 22:40
  • Possible duplicate of [How to access object properties with names like integers?](http://stackoverflow.com/questions/10333016/how-to-access-object-properties-with-names-like-integers) – miken32 Feb 22 '17 at 22:45
  • It does that's why you can't use `1invalidName`. `{$array[0]}` is just faking it out. I think `$object->{'1invalidName'}` will work as well. – AbraCadaver Feb 22 '17 at 23:20

0 Answers0