What is the purpose of {} in the code below? and what is the definition?

9
$objeto->{'getEndereco' . ucfirst($tipo)}()->getCep();
    
asked by anonymous 04.07.2017 / 14:59

3 answers

9

Official Documentation

It basically serves to define the beginning and the end of the method name that should be invoked. As the name varies by the value of $tipo , just do:

$objeto->'getEndereco' . ucfirst($tipo)()->getCep();

It will generate a syntax error, because PHP will not know what to do with a constant after the -> operator.

  

Syntax error, unexpected '' getEndereco '' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

Notice that the error itself says that a T_STRING would be expected, which in this case will be only getEndereco , without quotation marks, or a variable or the { character. That is, the {} characters indicate to PHP to interpret the internal value as the method name and not as any constant.

Run the displayed code:

$objeto->{'getEndereco' . ucfirst($tipo)}()->getCep();

It's analogous to doing:

$endereco = call_user_func([$objeto, 'getEndereco' . ucfirst($tipo)]);
$endereco->getCep();

However, in certain situations it is more convenient to only chain calls using {} . It is also possible to chain this notation. For example:

$foo = "Hello";
$bar = "foo";

echo "${${'bar'}} world"; // Hello world

Because% of% is interpreted as {'bar'} , then% of% is treated as the variable bar , whose value is ${'bar'} . Then, $bar will be interpreted as "foo" and {${'bar'}} will be treated as the foo variable, displaying its content, ${${'bar'}} .

In PHP 7+ versions you can also use $foo together with "Hello" to import multiple classes from the same namespace :

use Meu\Namespace\{ClasseA, ClasseB, ClasseC};

Previous versions would require:

use Meu\Namespace\ClasseA;
use Meu\Namespace\ClasseB;
use Meu\Namespace\ClasseC;
    
04.07.2017 / 15:12
4

This is to access the object dynamically. Let's assume that $ type has the following content:

$tipo = 'Residencial';

// Forma correta dinâmica
$objeto->{'getEndereco' . ucfirst($tipo)}()->getCep();

// Forma correta direta
$objeto->getEnderecoResidencial()->getCep();

// Todas as duas opções acima irão acessar a função do objeto e obter o getCep:
$objeto->getEnderecoResidencial()->getCep;

// Formas INcorretas
$objeto->'getEndereco' . ucfirst($tipo)()->getCep();
$objeto['getEndereco' . ucfirst($tipo)]()->getCep();
$objeto['getEnderecoResidencial']()->getCep();
    
04.07.2017 / 15:11
4

In PHP there are N ways to write a string , one of them is called Sintaxe complexa .

The name is not really due to the complexity of the syntax, but rather because of the complex expressions that can be written this way.

According to the documentation itself:

  

Any scalar variable, element of an array or property of a   object with a representation of a string can be included with this   syntax. Simply write the expression the same as   would appear outside of the string and then put it between {e}. Since that {   can not be escaped, this syntax will only be recognized when $   immediately follow the {. Use {\ $ to get a literal {$.

    
04.07.2017 / 15:19