I'm writing a function in an object and need to access an array that gets constructed when the object is created. I can access normal variables inside the function using $this->var but I can't access an array in the same way using $this->array['key']. Why can't my function use arrays?
Here's the offending code:
class User
{
public $_x;
public $_y;
public $_z;
public $_array;
public function __construct($username)
{
$_x = 'a';
$_y = 'b';
$_z = 'c';
$_array = array( 'red' => $_x, 'blue' => $_y, 'green' => $_z,);
}
public function myFunction()
{
echo $this->_x . "<br>";
echo $this->_y . "<br>";
echo $this->_z . "<br>";
echo $this->_array['red'] . "<br>";
echo $this->_array['blue'] . "<br>";
echo $this->_array['green'] . "<br>";
var_dump($this->_array);
}
}
$user = new User;
$user->myFunction();
Which displays:
a
b
c
NULL