tags:

views:

25

answers:

2

i have this code

$test = new test();
$test->var_test = array('one','two');


class test{

    var $var_test = array();


    function __construct(){

     var_dump($this);

    }


}

the var_dump give me the $var_test in null why; i give it the one two values

+2  A: 

No, you added the values later than the __construct() fired.

class test{
    var $var_test = array();
    function __construct($vars)
    {
        $this->var_test = $vars;
        var_dump($this);
    }
}
new test(array('one','two'));

will working.

fabrik
and so what is the use full to aff var $var_test = array(); in the start
moustafa
sorry maybe i don't understand your question. $var_test can be anything that you want. some kind of config variables used in your class for example.
fabrik
+2  A: 

No, the output of this is not null, the output is this:

object(test)#1 (1) {
  ["var_test"]=>
  array(0) {
  }
}

which means that var_test is an empty array.

The constructor (__construct function) is called at line one, and on line two you assign things to your array. Therefore, when constructor runs, the array has not been populated yet, and is therefore empty.

Palantir
and how i can populate it; and i think adding var = xx; in the start should do that
moustafa