0
class MyTest{

    public $array1;
    public $array2;


    public function __construct()
    {
        $this->$array1 = array();
        $this->$array2 = array();
        echo "Hello, I'm constructor<br/>";
        $this->m1();
    }

    function m1(){

        echo 'inside function';

        for($i=0;$i<10;$i++){
            $this->$array1[] = $i;
        }

        echo '<br/>Array 1<br/>';
        print_r(array_values($this->$array1));

        echo '<br/>Array 2<br/>';
        print_r(array_values($this->$array2));

    }

}

new MyTest;

This is the output:

enter image description here

Can anybody tell me why this happen :-( ?

Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
  • Since you asked this question, I assume you don't have error reporting enabled in PHP, or you would have seen a bunch of notices that might have helped you figure out what was going on with this. Check into [enabling error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) for your development environment. – Don't Panic Aug 10 '17 at 23:54
  • @Don'tPanic, very helpful information. Thank you. – Thilina Chamath Hewagama Aug 10 '17 at 23:57
  • No problem. Good luck! :) – Don't Panic Aug 10 '17 at 23:57

1 Answers1

1

Don't put dollar signs on references to your member variables.

$this->$array1
$this->$array2

Should be

$this->array1
$this->array2
Rob Ruchte
  • 3,569
  • 1
  • 16
  • 18