It seems like class variables can be added dynamically at any time,
but I have yet to find documentation that says this this legal.
I would hate to write a bunch of code to take advantage of this
"feature" only to find out it was a bug and fixed in the next release
of PHP.
EXAMPLE:
class foo
{
var $a; // <<< variable 'a' declared
function foo()
{
$this->a = "test";
}
};
class foo2
{
function foo2()
{
$this->a = "test"; // assignment to undeclared class variable 'a'
}
};
$foo_with = new foo();
$foo_without = new foo2();
echo "<pre>" .var_dump($foo_with). "</pre>";
echo "<pre>" .var_dump($foo_without). "</pre>";
OUTPUT:
object(foo)(1) { ["a"]=> string(4) "test" }
object(foo2)(1) { ["a"]=> string(4) "test" }
What I'm trying to understand is, what are the advantages of declaring class variables, since you can dynamically add them at anytime