Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does PHP operator ->{...} mean?

Tags:

operators

php

I recently saw this line in a PHP piece of code:

$dbObject = json_decode($jsonString);
$dbObject->{'mysql-5.4'}[0]->credentials

What does this mean? In the PHP docs we can read, that

Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).

But how can the Object $dbObject be defined to allow ->{...}[...]access? Is this code kind of unsafe? Which PHP version does allow this?

Did I miss anything in the PHP docs?

like image 893
WeSee Avatar asked Oct 01 '15 08:10

WeSee


4 Answers

It's to enable access to properties which would be invalid syntax as bare literals. Meaning:

$dbObject->mysql-5.4[0]->credentials

This is invalid/ambiguous syntax. To make clear to PHP that mysql-5.4 is a property and not a property minus a float, you need to use the {'..'} syntax.

To be exact, ->{..} enables you to use any expression as the property name. For example:

$dbObject->{ sprintf('%s-%.1f', 'mysql', 5.4) }
like image 108
deceze Avatar answered Sep 29 '22 17:09

deceze


The curly brace syntax allows you to use a string literal or variable as a property or method name.

This is useful for several reasons:

  1. (per @Deceze's answer): It allows you to access property names that would otherwise not be valid PHP syntax -- eg if the property name contains a dot or dash, per your example. Typically properties like this would be accessed via a __get() magic method, since they would also be invalid syntax to define as an actual property within the class.

  2. (per @feela's answer): It allows you to use variables to reference your properties. This is similar to the $$ variable variable syntax. It's generally not really very good practice, but is useful in some cases.

  3. (not mentioned by any other answers yet): It allows you to remove certain potential ambiguities from your code. Consider the following code for example:

    $output = $foo->$bar['baz'];
    

    This is ambiguous. You can resolve the ambiguity by adding braces or brackets:

    $output = $foo->{$bar['baz']};  //gets the value of a property from $foo which has a name that is defined in $bar['baz']
    $output = ($foo->$bar)['baz'];  //gets the ['baz'] element of $foo->$bar array.
    

    This is particularly important because the forthcoming relese of PHP 7 will change the default behaviour of code like like. This will make the language behave more consistently, but will break existing code that doesn't have the braces.

    See also the PHP documentation for this change here: https://wiki.php.net/rfc/uniform_variable_syntax

    But even without the language change to force the issue, code like this should have braces anyway to help with readability -- if you don't put braces, then you may end up struggling to work out what you were trying to do when you come back to the code in six months' time.

like image 45
Simba Avatar answered Sep 29 '22 17:09

Simba


The curly braces syntax in the example is not from the array syntax, but comes from a possible syntax to access variables with variable names.

Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of mulitple parts, or when the property name contains characters that are not otherwise valid (e.g. from json_decode() or SimpleXML).

Examples from the PHP docs:

echo $foo->{$baz[1]} . "\n";
echo $foo->{$start . $end} . "\n";
echo $foo->{$arr[1]} . "\n";

See “PHP Variable variables”: http://php.net/manual/en/language.variables.variable.php (More examples on the usage there…)

like image 4
feeela Avatar answered Sep 29 '22 17:09

feeela


As an addition to @deceze's answer:

The -> operator means that you are accessing an object property. In this case the property name should be mysql-5.4 which is not valid PHP identifier (it must contain letters, numbers or underscores, see here). So it is 100% sure that the property name has been created dynamically.

PHP allows overloading properties using magic method called __get(). The body of this method allows you to handle any property you would wish - this can be any string, or any variable, even an object, which is cast to a string.

So in your case somebody created a class with a __get() magic method that handles string mysql-5.4. The curly braces { and } are given to denote that the string should be treated as property name (however there is no property with this name).

like image 4
Voitcus Avatar answered Sep 29 '22 16:09

Voitcus