I have been searching for a while but I could not find the answer, what are the differences between these two ways of inicialize a variable class in PHP?:(if there are)
class MyClass
{
private $myVariable='something';
public function __construct()
{
}
}
class MyClass
{
private $myVariable;
public function __construct()
{
$this->myVariable='something';
}
}
See this scenario:
This is a difference between the two calls. parent::__construct() is optional for child classes that inherit from a parent. So:
is_scalar()) properties that need to be preset, do it in the class definition to be sure that they exist in child classes too.It all depends on how you design your code’s functionality.
There’s no right of wrong here, it’s only what’s right for you.