$ is the name of the vs function

I get OOP support and finally started creating them in my scripts. One thing that I don’t understand is "$ this" after instantiating the class. For example, this guy is encoded:

class Form
{
protected $inputs = array();    

public function addInput($type, $name)
{
     $this->inputs[] = array("type" => $type,
            "name" => $name);
}


}

 $form = new form();

 $this->addInput("text", "username");
 $this->addInput("text", "password");

Note that the last two lines show that he used $ this-> addInput ().

How is it different from $ form-> addInput? I have always used the name of the variable that I use to create an instance of the class. I do not see what the function $ this-> function () does. How does PHP know which object it refers to?

From what I understand, $ this-> var is used in any methods inside this object. If there is no $ this-> var, but a rather simple variable $, then it cannot be used in other methods except the method that has this variable $, right?

: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me/3689613#3689613

+3
1
// Incorrect
$this->addInput("text", "username");
$this->addInput("text", "password");

. $this, . $form. , : , $form->addInput $this->addInput !

// Correct
$form->addInput("text", "username");
$form->addInput("text", "password");

, , , . . !

+3

Source: https://habr.com/ru/post/1769922/


All Articles