I am getting a hang of OOP and finally started creating them in my scripts. One thing I don't get it is the "$this" after creating an instance of class. For example, this guy coded:
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");
Notice that the last two lines show that he used $this->addInput().
How is it different from $form->addInput? I always used the name of variable I use to create an instance of class. I don't see what $this->function() does. How does the PHP know which object it's referring to?
From what I understand, $this->var is used in any methods within that object. If there's no $this->var but rather plain $variable then it cannot be used in other methods other than the method that has that $variable, correct?
related: http://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me/3689613#3689613