Situation

class test   {  
        private $data = array();        
        public function __get($name) {
            return isset($this------->data[$name]) ? $this->data[$name] : null;
        }

        public function __set($name, $value) {
            $this->data[$name] = $value;
        }

        public function run() {
            $this->test = array('first' => 1);
            $this->test['second'] = 2;
        }
}

$test = new test();
$test->run();

 // OUTPUT :
 // PHP Notice:  Indirect modification of overloaded property test::$test has no effect in /data/test/test22.php on line 13

The problem

You see “test” is not a property of this class. So it’s calling the Magic Method __get() but since it’s return by value, the modification on line 26 has no effect. You will have this problem for array and object.

Solutions

  1. Instead of adding a new item directly, you could use array_merge or use the + operator
  2. You could return __get by reference
  3. You could specify the property directly in the class
   // solution #1
   $this->test += array('second' => 2);
   $this->test = array_merge($this->test, array('second' => 2);

   // solution #2
   public function &__get($name) { ... }

   // solution #2
   private $test = array(); // With the data property

List of related links

CAUTION : PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.