Page 1 of 1

Adding Class Properties At Runtime

Posted: Fri Sep 16, 2005 2:39 am
by harrisonad
Hi, I observed this morning that I can add additional class properties at runtime, without any error produced.
Is it normal or a bug?

Code: Select all

class A
{
    var $var1;		

    function AddVar2(){
        $this->var2 = 0;
	}
}

$b = new A;
print_r($b);
echo "<br>";

$b->AddVar2();
print_r($b);
This will output
My Screen wrote: a Object ( [var1] => )
a Object ( [var1] => [var2] => 0 )
I'm using PHP 4.4.0

Posted: Fri Sep 16, 2005 5:20 am
by dbevfat
It's quite normal for "soft" languages like PHP. By doing that you define a public property. This has a good side (allows more dynamic aproaches) and a bad side (the property does not exist in class definition and documentation).

Regards