Thread: New approach
View Single Post
Old 05-06-2008, 06:42 PM   #11 (permalink)
sketchMedia
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

A singleton is an object that can only exsist once (normal classes can have many instance of the same type), this is designed to provide a 'global' point of access to the singleton object instance amongst other things.

The registry pattern doesnt have to be a singleton all it does is provide a single place to hold system vars (much like the windows registry); however combining these two patterns together can be very powerful for example instead of having to pass the registry object around in class contructors you can use a global 'getter' i.e.
PHP Code:
//dummy registry using a singleton
class registry
{
    private 
$pSelf;
    private function 
__contruct() {}
    private function 
__clone() {}
    public static function 
getInstance()
    {
          if(!
self::$pSelf instanceof self)
          {
               
self::$pSelf = new self;
           }
           return 
self::$pSelf;
    }

therefore:
PHP Code:
class test
{
    private 
$registry;
    public function 
__construct(registry $registry)
    {
        
$this->registry $registry;
    }
}
$registry = new registry();
$test = new test($registry); 
would be replaced by :
PHP Code:
class test
{
    private 
$registry;
    public function 
__construct()
    {
        
$this->registry registry::getInstance();
    }
}
$test = new test(); 
Btw in an example someone posted previoulsy contianed PHP5 E_STRICT errors because of the use of the '&' reference operator; PHP5 passes objects automatically by reference thus '&' in this context has been depreciated and produces an E_STRICT warning.


Edit: due to my brain not working properly most of the above mentioned code wont produce errors although using '&' to get a reference of an object is not needed as of PHP5, '$obj =& new obj();' will however return a strict error
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 05-06-2008 at 07:37 PM.
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
delayedinsanity (05-06-2008)