I want to use a Registry to store some objects. Here is a simple Registry class implementation.
<?php
final class Registry
{
private $_registry;
private static $_instance;
private function __construct()
{
$this->_registry = array();
}
public function __get($key)
{
return
(isset($this->_registry[$key]) == true) ?
$this->_registry[$key] :
null;
}
public function __set($key, $value)
{
$this->_registry[$key] = $value;
}
public function __isset($key)
{
return isset($this->_registry[$key]);
}
public static function getInstance()
{
if (self::$_instance == null) self::$_instance = new self();
return self::$_instance;
}
}
?>
When I try to access this class, I get “Indirect modification of overloaded property has no effect” notification.
Registry::getInstance()->foo = array(1, 2, 3); // Works
Registry::getInstance()->foo[] = 4; // Does not work
What do I do wrong?
This behavior has been reported as a bug a couple times:
It is unclear to me what the result of the discussions was although it appears to have something to do with values being passed “by value” and “by reference”. A solution that I found in some similar code did something like this:
Notice how they use
&__getand&__setand also when assigning the value use& $value. I think that is the way to make this work.