Trying to understand parameters in classes
Posted: Fri Nov 07, 2014 6:54 pm
I am going though the course right now on php and I am trying to understand the class structure. I understand that the __construct magic method runs first thing when your object is created. The class I am taking created a random generator to explain some concepts. what I don't understand is why declare $number outside of the construct when you could declare it inside or just make a function and declare it there?
I am guessing this is because variables declared inside a function are only available in that function and those declared outside are available to any function. Is this a correct understanding?
Code: Select all
class Number {
public $number;
function __construct()
{
$this->number = rand(1, 10);
echo $this->number;
echo "<br />";
ob_flush();
flush();
sleep(1);
unset($this);
}
public function __destruct()
{
$number = new Number;
}
}
$number = new Number;