TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Registry pattern (http://www.talkphp.com/advanced-php-programming/2023-registry-pattern.html)

xenon 01-20-2008 12:49 AM

Registry pattern
 
Heard about it? Used it? I've already implemented it and it's a life saver. ^^ I bet there are many more interesting patterns to follow, but I really needed this one, so I could stop passing an object over and over again through the constructors and such and I didn't even know it exists :P

My implementation:

PHP Code:

final class Registry
{
    
/**
     * The internal class library
     *
     * @var array
     */
    
private static $library;

    
/**
     * No need for a constructor.
     */
    
private final function __construct() { }

    
/**
     * Returns an object reference from the library
     *
     * @param string $name
     * @return object|null
     */
    
public static function &get$name null )
    {
        
self::_init();

        if( empty( 
$name ) )
        {
            return 
null;
        }

        if( 
array_key_exists$nameself::$library ) )
        {
            return 
self::$library$name ];
        }
    }

    
/**
     * Sets a reference to an object in the internal library
     *
     * @param string $name
     * @param object $obj
     */
    
public static function set$name null$obj )
    {
        
self::_init();

        if( empty( 
$name ) )
        {
            return;
        }

        
self::$library$name ] = $obj;
    }

    
/**
     * Initialize the library
     */
    
protected static function _init()
    {
        if( empty( 
self::$library ) )
        {
            
self::$library = array();
        }
    }


And by the way, "Design patterns" from Wordware is well worth reading. An excelent object oriented reference book.

xenon 01-21-2008 11:48 AM

Oh, come on...nobody uses it? Here's an example of how easy your life becomes:

index.php:
Code:

$database = new Database();

// ... some more code

require 'registry.php';
Registry::set( 'db', $database );

somelibrary.php (index.php dispatches this file):

Code:

require 'registry.php';

class Test
{
  protected $database;

  public function __construct()
  {
        $this->database = Registry::get('db');

        // do stuff with the db
  }
}

So, no more global declarations, no more passing through constructors/methods or such. Don't you think this is useful? Ok then, how about some other useful patterns you use and you think are worth delving into? (besides the Singleton of course)

RobertK 01-21-2008 03:18 PM

I haven't yet, but I was considering the Registry method for configuration and loading specific settings. Wordware's book on design patterns is my next book; I've been reading about OOA&D this past week or two, and letting that sink in first.

sketchMedia 01-21-2008 03:25 PM

yea i've used it before, i used it for a simple mvc based site i did not long ago, pretty good

JREAM 12-04-2009 12:53 AM

Xenon thank you for this post it is a great example and exactly what I was trying to understand. You provided the usage of Registry within another class and I needed to see a practical example!

Cheers

vpopp 12-04-2009 01:03 AM

I am new here...is your post regarding my cry for help with sql comma problem? If so...that sql is way over my head!

ETbyrne 12-04-2009 08:34 PM

Stay on topic vpopp. People will respond to your sql thread if they have a solution. This is not the place to talk about that.

ChrisR 12-10-2009 02:44 AM

I use this method all the time i am working on a site right now using something like this. I think its good.


All times are GMT. The time now is 07:31 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0