$this->name is a property, but what next? Those. How to understand such long constructions? I saw in the code of frameworks and in someone else's code, but no one in the video tutorials that came across to me has ever shown such a long construction and what exactly is happening in it.

Another example: $this->data->name = $name;
What does it mean: ->data->name

  • read the comments here . Somewhere there should be a normal answer, but to look for, frankly, laziness) - BOPOH

2 answers 2

This is a composition, or association. The case when the class field is another class that has its own fields and methods.

For example:

 class foo { public $nameFoo; public someMethod() { return "какое то значение"; } } class bar { public $nameBar; __construct() { $this->nameBar = new foo(); } } 

Then a call to the nameFoo field of the nameFoo field (which is an object of the foo class) will look like this:

 $item = new bar(); //обращаемся к полю (nameFoo будет присвоена строка "присваиваемое значение") $item->nameBar->nameFoo = "присваиваемое значение"; //обращаемся к методу (будет возвращена строка "какое то значение") $item->nameBar->someMethod() 

And there may be many such nestings.

  • All clear. Thank you all very much :) - Alex
  • one
    @Alexey @totorro I suppose that not only a class is a field of another class, but in general an OBJECT is a class field ... as an example .. you can make an associative array, convert it to an object and assign it to a class field .. as an example ideone.com / 6WXLOl ... so I made an object from an array .... now this object can be entered into $this->field - Alexey Shimansky
  • Alexey Shimansky - thank you too :) - Alex

In the video this does not tell. Because the authors of the video lessons themselves do not know anything, but only how the monkeys repeat a couple of teams learned from another such dunce.

The explanation is very simple: in PHP you can always refer to the result returned by the variable. For example, the record type

 $name = $array[1]["name"]; 

means that we first accessed the $array[1] variable, but it is also an array, and we can immediately refer to its element ["name"]

The same with objects. If the expression returns an object, then we can draw an arrow and access its property or method. And so on to infinity. In English, this is called method chaining, that is, stringing one method onto another. For example, in PDO you can write this:

 $id = $pdo->query("INSERT INTO t VALUES(NULL)")->insertId(); 

Accordingly, you can write $this->data->name only if data is an object.