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