S. Blaise S. Blaise - 1 year ago 85
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 ?

Answer Source

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";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download