Confusion about returning by reference vs. by value

<?php
class Config { 
    private $values = []; 
    public function getValues() {
        return $this->values; 
    }
} 
$config = new Config(); 
$config->getValues()['test'] = 'test'; 
echo $config->getValues()['test'];
?>

If you run the above code, you’ll get the following: PHP Notice: Undefined index: test in path_of_your_file on line n

The issue is that the above code confuses returning arrays by reference with returning arrays by value. Unless you explicitly tell PHP to return an array by reference (i.e., by using&), PHP will by default return the the array “by value”. This means that a copy of the array will be returned and therefore the called function and the caller will not be accessing the same instance of the array.

<?php
class Config { 
    private $values = []; 
// return a REFERENCE to the actual $values array 
    public function &getValues() {
        return $this->values;
    }
} 
$config = new Config(); $config->getValues()['test'] = 'test'; 
echo $config->getValues()['test'];
?>

https://www.toptal.com/php/10-most-common-mistakes-php-programmers-make