I need to change an array from outside the class, which is a class variable. For example see my code below:
<?php
class ArrayRet
{
private $_ar = array();
public function __construct()
{
$this->_ar['test1'] = 'hello1';
$this->_ar['test2'] = 'hello2';
}
public function getAr()
{
return $this->_ar;
}
public function dump()
{
var_dump($this->_ar);
}
}
$arRet = new ArrayRet();
$arRet->dump();
$ar = $arRet->getAr();
$ar['test2'] = 'works!!!';
$arRet->dump();
?>
$this->_ar['test2'] should be works!!! but it´s still hello2
How can I get this working?
Edit: With changing the syntax a bit as found here, it works. So, we need to change the method to this:
public function &getAr()
{
return $this->_ar;
}
And also the call to this:
$ar = &$arRet->getAr();