I saw in the book such a method in the class, for which properties are wrapped in curly braces?

function pechat(){ echo "{$this->name} "."{$this->lastname}"; } 
  • four
    to understand whether you need a variable $ a followed by an arrow or a property in the entry "$a->b" - splash58

2 answers 2

When using double quotes, it often happens that the variable should be used in a slightly modified form. But when analyzing a string, PHP cannot determine that it is a variable. To solve this problem, wrap the variable in curly braces.

 <?php $juice = 'plum'; echo "I drank some juice made of $juices"; // $juices не определена vs. $juice = 'plum'; echo "I drank some juice made of {$juice}s"; // $juice будет анализирована. /** * Комплексные переменные также оборачивайте в фигурные скобки. */ $juice = array('apple', 'orange', 'plum'); echo "I drank some juice made of {$juice[1]}s"; // $juice[1] будет анализирована. 

Taken here

The same goes for class fields and heter methods

Ie, if you remove the quotes, then at best (this is if you have the __toString () method defined in your class) you will get something like this

 "строкаКоторуюВозвращает__toString->name строкаКоторуюВозвращает__toString->lastname" 

In the worst case - get something in style, and it will most likely be

ErrorException Object of class Your Class could not be converted to string

And again, since you use double quotes, concatenation is superfluous. You can write this:

 echo "{$this->name} {$this->lastname}"; 

    Braces are needed for grouping variables. How does the interpreter understand - is it a property of a class or variable that should be output before the string "-> name"?

    This is the basis of the basics. I always advise you first to study the documentation and at least search before asking a question.