为什么数组在PHP中为NULL?

<?php    

class ffooo
{
    public $arr;

    function __construct()
    {
        $arr=array();
    }

    function add($val)
    {
        $arr[]=$val;
    }

    function get($ind)
    {
        return $arr[$ind];
    }
}

$cont=new ffooo();
$cont->add("derek",'chmo');
echo $cont->get(0);
var_dump($cont);

Can anybody explain me why my array $arr is NULL after method add($val)? I try to echo array $arr in method "add",and in this method $arr contained come value; but in another method it becomed NULL? What is the magic?I do't understand the logic(

Because it is defined locally only. To use the class member, you must use $this;

$this->arr

That's because you declare a variable $arr every time. And in every method it's just new, as functions have their own scope.

You need to set a property, like that: $this->arr = array(...);. Properties exist in object scope, so they are accessible from every method.

You declared the function with one argument:

function add($val) {}

But pass to it two ones. And why do you use local copies of the class property in every function? Correct your code before talking about magic :)

Use $this->arr instead of $arr in all methods' bodies.

You forgot to use $this, please see the code below.

<?php    

class ffooo
{
    public $arr;

    function __construct()
    {
        $this->arr = array();
    }

    function add($val)
    {
        $this->arr[] = $val;
    }

    function get($ind)
    {
        return $this->arr[$ind];
    }
}

$cont=new ffooo();
$cont->add('derek');
echo $cont->get(0);
var_dump($cont);