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 :-( ?

3
  • 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 for your development environment. Commented Aug 10, 2017 at 23:54
  • @Don'tPanic, very helpful information. Thank you. Commented Aug 10, 2017 at 23:57
  • No problem. Good luck! :) Commented Aug 10, 2017 at 23:57

1 Answer 1

1

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

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

Should be

$this->array1
$this->array2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.