Page 1 of 1
Simple Registry Class
Posted: Fri Oct 20, 2006 7:14 pm
by LiveFree
Code: Select all
<?php
/*
* @file = 'Registry.php'
* @location = /
*/
class Registry extends CodeBlocks {
/*
* @variable
* $objects (array)
* @access protected
*/
protected $objects = array();
/*
* @function
* "set"
*
* @param $object (object) #Object to be added
* @param $name (string) #Name of the object
* @return void
*/
public function set($object, $name) {
if (!is_object($object)) {
throw new RegistryException('Invalid Paramater 1 Passed to set()');
return;
}
if (in_array($object, $this->objects)) {
throw new RegistryException('Invalid Paramater 2 Passed to set()');
return;
}
$this->objects[$name] = $object;
return;
}
/*
* @function
* "get"
*
* @param $name (string) #Name of object to return
* @return (object) #The returned object
*/
public function get($name) {
if (!array_key_exists($name, $this->objects)) {
throw new RegistryException('Invalid Object Name passed to get()');
return;
}
return $this->objects[$name];
}
/*
* @function
* "ifNameExists"
*
* @param $name (string) #String you want to check for
* @return boolean #T/F based on search
*/
public function ifNameExists($name) {
if (array_key_exists($name, $this->objects)) {
return true;
} else {
return false;
}
}
/*
* @function
* "ifObjectExists"
*
* @param $object (object) #Object you wish to check for
* @return boolean #T/F based on search
*/
public function ifObjectExists($object) {
if (in_array($object, $this->objects)){
return true;
} else {
return false;
}
}
/*
* @function
* "clearObjects"
*
* @return void
*/
public function clearObjects() {
$this->objects = array();
return;
}
}
?>
(NOTE: The comments are using my own documentation style, I am not trying to copy PHPDoc's)
Its supposed to be very simple, no bells and whsitles... it does the job. But I would like any ways to optimize it.
Thanks!
Posted: Fri Oct 20, 2006 7:34 pm
by Christopher
Well ... the main omissions are the CodeBlocks class and the RegistryException class. I think better error messages would be nice. And I think I would call ifNameExists() and ifObjectExists() something simpler like has() and hasInstance().
Posted: Sat Oct 21, 2006 4:55 pm
by John Cartwright
Code: Select all
public function ifNameExists($name) {
if (array_key_exists($name, $this->objects)) {
return true;
} else {
return false;
}
}
/*
* @function
* "ifObjectExists"
*
* @param $object (object) #Object you wish to check for
* @return boolean #T/F based on search
*/
public function ifObjectExists($object) {
if (in_array($object, $this->objects)){
return true;
} else {
return false;
}
}
Can be reduced to
Code: Select all
public function ifNameExists($name) {
return array_key_exists($name, $this->objects);
}
/*
* @function
* "ifObjectExists"
*
* @param $object (object) #Object you wish to check for
* @return boolean #T/F based on search
*/
public function ifObjectExists($object) {
return in_array($object, $this->objects);
}
Posted: Sat Oct 21, 2006 5:24 pm
by timvw
Why do you have return statements after a throw statement?
As the manual says (
http://be2.php.net/throw):
When an exception is thrown, code following the statement will not be executed.
Posted: Sat Oct 21, 2006 5:25 pm
by LiveFree
Thanks JCart!
@arborint: I'll re-do my Exceptions messages and refactor and I'll re-post a bit later.
Thanks for the advice
Posted: Fri Nov 03, 2006 7:58 am
by DaveTheAve
I'm not trying to seem mean or anything; however, why would one want to use a Registry class? I have always wondered this, if you can give me a good reason, I might implement on into my current project.
Note: I asked the same thing about a Session Handler but as you'll soon find out, I ended up making my own I use over the PHP Session handler. (To be posted in Coding Critique - Not Finished)
Posted: Fri Nov 03, 2006 8:05 am
by Jenk
The Registry provides a means of loose coupling objects. Tight, or Hard coupling is bad between objects that are not going to be coupled in every possible circumstance, so instead we use a registry to provide us with a respository of, usually, singleton objects that can be used through the application.
Posted: Fri Nov 03, 2006 8:22 am
by Chris Corbyn
Using a registry among other things means that you don't need to pass one object from object A to object B, then from object B to object C etc. You just keep a reference to it in the registry.
You can often use a registry to "register" an object for re-use rather than usiing a singleton too. Registries are extremely useful, and they come in many variants.