SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Nov 2004
    Location
    Ithaca, NY
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Registry class -- will this work?

    I'm pretty new to the whole idea of designing with patterns, so please correct me if I start to sound clueless here. But it seems that my application calls for a singleton registry, because I'll be using a single global instance of a few different objects, and thanks to help here I think I've got a way to do it. I'm just wondering if anyone sees obvious reasons why this won't work, or if it throws up any red flags or anything...

    Here's Registry.php:

    Code:
    <?
    class Registry {
    
    	var $cache;
    
    	function Registry() {
    		$this->cache = array();
    		}
    
    	function setEntry($key, &$item) {
    		$this->cache[$key] = &$item;
    		}
    
    	function isEntry($key) {
    		return ($this->getEntry($key) !== null);
    
    	function &get($key) {
    		static $registry;
    		if ($registry && $key) {
    			return $registry->cache[$key];
    			} else {
    			$registry = new Registry();
    			return $registry;
    			}
    		}
    	}
    
    $r = Registry::get();
    $r->setEntry('database', new DatabaseObject());
    $r->setEntry('session', new SessionObject()); 
    
    ?>
    Then, in my other classes, I'd call it something like this:

    Code:
    function doSomething(){
    
    	$database = Registry::get('database');
    	$session = Registry::get('session');
    
    	// Do stuff with them.
    
    	}

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like your "get()" method is a mixture between a getKey() and a getInstance() that I would avoid.

    Also, with PHP4 you always want to catch the objects by reference:
    PHP Code:
    $database =& Registry::get('database');
        
    $session =& Registry::get('session'); 
    Also, with error_reporting(E_ALL) I suspect you will get some errors if you try to reference a key you have not yet set, I would code some array_key_exists($key, $this->cache)s in these in a few spots.

    And, lastly, PHP4 does not store references in static variables. See my post #4 in this thread for more details.

    HTH
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    United Kingdom
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A did a similar thing with PHP 5 (didn't actually know there was a design pattern for this). Anyway, in PHP 5 you can take advantage of static member variables which makes the whole thing alot easier. Things are a bit more tricky with PHP 4. Hope it helps a bit.
    PHP Code:
    abstract class Repository
    {
        
        static private 
    $repository = array();
        
        
    /**
        *    Returns a static instance of $class
        *    @param string $class
        *    @return obj $$class
        */
        
    final static function getInstance($class$arg NULL)
        {
            static 
    $repository;
            
            if(
    is_array($arg))
            {
                
    $arg implode(","$arg);
            }
            if(!isset(
    Repository::$repository[$class]))
            {
                
    Repository::$repository[$class] = new $class($arg);
            }
            return 
    Repository::$repository[$class];
        }
    }
    // Requests the current Singleton, or creates a new one.
    Repository::getInstance("SqlQueryHandler"); 

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to search theese forums for Singleton

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Kansas City, MO
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would suggest taking a look at phpPatterns Registry article. That should give you a pretty good insight as to what to do. It also has a fully ready to use PHP4 Registry class as part of the examples.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •