S. Blaise S. Blaise - 6 months ago 14
PHP Question

PHP operator $foo-> {''}

I noticed that on php with the JSON lib we can access an element with a syntax i haven't seen before.

$jsonObject->{'myvar'}; // return the value of myvar on the jsonobject

How it works ?


I do not think it is inherent with the JSON lib.

It's just another way to determine the name of the variable you want to call. For example, it's useful when you want to decide dynamically which variable should be called, something that is of great use in magic methods.

$var->{"var_name"} is the equivalent of $var->var_name.

You can do things like these as well:

$key = "key_name";


You can also access keys with invalid characters such as dots or commas.

$key = "key.name.nice";