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

Asked

Viewed 220 times

10

$objeto->{'getEndereco' . ucfirst($tipo)}()->getCep();
  • It seems you want to do this: $object->getEnderecoComercial()->getCep();

  • exact, but what is the definition? and is a good practice?

3 answers

9


Official documentation

It basically serves to define the beginning and end of the method name that must be invoked. How the name varies according to the value of $tipo, do only:

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

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

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

See that the mistake itself says that one would be expected T_STRING, which in this case will be getEndereco, without quotation marks, or a variable or 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();

Is analogous to do:

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

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

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

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

For {'bar'} is interpreted as bar, then ${'bar'} is treated as the variable $bar, whose value is "foo". Soon, {${'bar'}} will be interpreted as foo and ${${'bar'}} will be treated as the variable $foo, displaying its content, "Hello".


In PHP 7+ versions it is also possible to use {} along with use to import multiple classes from the same namespace:

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

In previous versions it would be necessary to do:

use Meu\Namespace\ClasseA;
use Meu\Namespace\ClasseB;
use Meu\Namespace\ClasseC;
  • I don’t know Java Regeneration, I’ll do a search, vlw.

  • @ALE_ROM, for more source on reflection in Java: https://stackoverflow.com/a/37632/4438007; but as Andersoncarloswoss and I chat, it is very forced to compare, until I removed my comment comparing

4

This serves to dynamically access the object. Let’s assume that the $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();

4

In PHP there is N ways of writing a string, one of them is the call Sintaxe complexa.

The name is not really due to the complexity of the syntax, but rather due to 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 that syntax. Simply write the expression in the same way as would appear outside the string and then put it between { e }. Since { cannot escape, this syntax will only be recognized when $ immediately follow {. Use { $ to get a literal {$.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.