Registry failure
Posted: Thu Sep 07, 2006 9:13 am
Hey, can anyone tell me why the following isn't working?
I was hoping that I could feed the registry anywhere in my script and that it's everywhere 'updated'. That's what pass-by-reference does, I thought.
Code: Select all
<?
class Registry {
var $objects = array();
function set($name, &$object) {
if(!is_object($object)) {
trigger_error('Non-object feeded to the registry', E_USER_ERROR);
}
$this->objects[$name] = &$object;
}
function &get($name) {
if(isset($this->objects[$name])) {
return $this->objects[$name];
}
}
function delete($name) {
if(isset($this->objects[$name])) {
unset($this->objects[$name]);
}
}
function &instance() {
static $reg;
if(!$reg) {
$reg = &new Registry();
}
return $reg;
}
}
class Class1 {
var $reg;
function Class1() {
$this->reg = &Registry::instance();
}
function checkReg() {
print_r($this->reg);
}
}
class Class2 {
var $reg;
function Class2() {
$this->reg = &Registry::instance();
}
function checkReg() {
print_r($this->reg);
}
}
$reg = &Registry::instance();
$reg->set('Class1', new Class1());
$reg->set('Class2', new Class2());
$Class1 = &$reg->get('Class1');
$Class1->checkReg();
$Class2 = &$reg->get('Class2');
$Class2->checkReg();
?>