Referring to this question: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me
class Form
{
protected $inputs = array();
public function makeInput($type, $name)
{
echo '<input type="'.$type.'" name="'.$name.'">';
}
public function addInput($type, $name)
{
$this->inputs[] = array("type" => $type,
"name" => $name);
}
public function run()
{
foreach($this->inputs as $array)
{
$this->makeInput($array['type'], $array['name'];
}
}
}
$form = new form();
$this->addInput("text", "username");
$this->addInput("text", "password");**
Can I get a better explanation of what the $this->input[] is doing in this part:
public function addInput($type, $name)
{
$this->inputs[] = array("type" => $type,
"name" => $name);
}
It’s accessing that varible for that instance of the class/object. So let’s say you create a new instance of the class by writing
$something = new Form();. Now when you use a function in the class by calling it with $something->functionname(); the function will refence to the $something instance when it say this. The great thing with objects like this is that the functions can access each others varibles.