Hi everyone,

I'm new to patterns but I just don't get why is the usage of Singletons in PHP so great. I can understand why to use them in Java, but PHP? Isn't a simple global variable the same?

For example imagine a Singleton DB layer usage.
PHP Code:
function fnc1(){
   
$db DBSingleton->getInstance();
   
$rows $db->getAll(...);
   
// ...
}

function 
fnc2(){
   
$db DBSingleton->getInstance();
   
$col $db->getOne(...);
   
$rows $db->getAll(...);
   
// ...

I've just don't see the difference and/or advatage in ease of use, flexibility, speed or code cleanliness from doing the simpliest thing
PHP Code:
function fnc1(){
   global 
$db;
   
$rows $db->getAll(...);
   
// ...
}

function 
fnc2(){
   global 
$db;
   
$col $db->getOne(...);
   
$rows $db->getAll(...);
   
// ...

I'm using something different, a simple class for PEAR::DB abstraction layer. I'm using it over half a year but yesterday i've figured out that it is in fact a Monostate pattern.

PHP Code:
function fnc1(){
   
$rows SQL::getAll(...);
   
// ...
}

function 
fnc2(){
   
$col SQL::getOne(...);
   
$rows SQL::getAll(...);
   
// ...

No global, no singleton and simplier code. So why use Singletons at all?