It's using PHP's hacked on property overloading to add entries to and retrieve entries from the private $vars
array.
To add a property, you would use...
$registry = new Registry;
$registry->foo = "foo";
Internally, this would add a foo
key to the $vars
array with string value "foo" via the magic __set
method.
To retrieve a value...
$foo = $registry->foo;
Internally, this would retrieve the foo
entry from the $vars
array via the magic __get
method.
The __get
method should really be checking for non-existent entries and handle such things. The code as-is will trigger an E_NOTICE
error for an undefined index.
A better version might be
public function __get($key)
{
if (array_key_exists($key, $this->vars)) {
return $this->vars[$key];
}
// key does not exist, either return a default
return null;
// or throw an exception
throw new OutOfBoundsException($key);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…