I had a scenario at work today that need me to use a variable in an object and call it like I would when using keys in an array.
Like when I have an array, I would do something like this:
$foo = array("apple" => "Hello World!"); $bar = "apple"; echo $foo[$bar]; //OUTPUT: Hello World!
I was able to use a variable for an object property by simply using $variable.
$obj = new stdClass(); $obj->bar= 'Hello World!'; $foo = "bar"; echo $obj->$foo; //OUTPUT: Hello World!
I thought it would spit out an error. But it didn’t