i wonder if there is any difference between
class TestClass {
private $_var = "abc";
}
vs
class TestClass {
private $_var;
function __construct() {
$this->_var = "abc";
}
}
i wonder if the latter is the preferred way/better practice? is there any functional difference?
They’re effectively the same. I prefer the former, because then there’s only one place to look for the value and its default.
On the other hand, if you need to do something dynamic with it or set it to anything other than an array or primitive, you need to use the second form. Notably, you can’t use a function call to declare a variable in the first form.