Why do I have to initialize variables in a class?
$test = new myclass();
class myclass {
private $var; // WHY THIS LINE?
public function sayHello() {
$this->var = 'hello';
echo $this->var;
}
}
$test -> sayHello();
When I remove the line with private $var; this example works too.
Why do I have to initialize variables in classes?
Initializing variables has some benefits:
It's good practice to declare your variables, but it isn't a requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With