在构造函数中分配数组参数[关闭]

Now i am coming in a circumstance like, i need to pass a parameter for array in a function(So that i can override it in the function based on my needs). So how is that possible in PHP. For eg.,

class classname
{
    function __construct()
    {
        $array['default'] = 'default value';
    }

    function test()
    {
        $array['a'] = 'a';
        $array['b'] = 'b';
    }
}

I need to get the $array['default'] in the function test. So how to do that in PHP? Any help on this would be greatly appreciated.

Try, $this

class classname
{
    public $array;

    function __construct()
    {
        $this->array['default'] = 'default value';
    }

    function test()
    {
        $this->array['a'] = 'a';
        $this->array['b'] = 'b';
    }
}

Looks like you want $array to be an object property:

class classname
{
    protected $array = array();

    function __construct()
    {
        $this->array['default'] = 'default value';
    }

    function test()
    {
        $this->array['a'] = 'a';
        $this->array['b'] = 'b';
    }
}

you have to define the array in the class only first

class test {

$array=array();

function __construct()
{
    $this->array['default'] = 'default value';
}

function test()
{
    $this->array['a'] = 'a';
    $this->array['b'] = 'b';
}

}