TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-19-2008, 11:49 PM   #1 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 440
Thanks: 3
xenon is on a distinguished road
Default 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.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.

Last edited by xenon : 05-05-2008 at 10:11 PM.
xenon is offline  
Reply With Quote
The Following 3 Users Say Thank You to xenon For This Useful Post:
drewbee (05-06-2008), jaegerschnitzel (02-15-2008), vpopp (12-04-2009)
Old 01-21-2008, 10:48 AM   #2 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 440
Thanks: 3
xenon is on a distinguished road
Default

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)
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-21-2008, 02:18 PM   #3 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

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.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-21-2008, 02:25 PM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 836
Thanks: 31
sketchMedia is on a distinguished road
Default

yea i've used it before, i used it for a simple mvc based site i did not long ago, pretty good
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-03-2009, 11:53 PM   #5 (permalink)
The Visitor
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
JREAM is on a distinguished road
Default

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
JREAM is offline  
Reply With Quote
Old 12-04-2009, 12:03 AM   #6 (permalink)
The Wanderer
 
Join Date: Dec 2009
Location: Honey Brook PA
Posts: 7
Thanks: 2
vpopp is on a distinguished road
Default

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!
vpopp is offline  
Reply With Quote
Old 12-04-2009, 07:34 PM   #7 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 12-10-2009, 01:44 AM   #8 (permalink)
The Wanderer
 
Join Date: Dec 2008
Posts: 15
Thanks: 2
ChrisR is on a distinguished road
Default

I use this method all the time i am working on a site right now using something like this. I think its good.
ChrisR is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:40 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design