Simple Registry Class

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Simple Registry Class

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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().
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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);
        }
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post 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
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post 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)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply