PHP6 suggestion: singleton
| From: | Marc Cools | Date: | Wed, 04 Mar 2009 00:19:00 +0000 |
| Subject: | PHP6 suggestion: singleton | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-3963@lists.php.net to get a copy of this message | ||
Hi,
I would like to make a suggestion for implemention in php6 (or php5.3.x) about singleton classes.
Now you have to write a function like ::getInstance to get the class. getInstance checks If the
class already exist, and if not it calls the private __construct function and so returning the
excisting or new object.
so now it is something like this:
class singletonClass{
private $_instance = null;
public function getInstance()
{
if ( null === $this->_instance )
{
$this->_instance = new singletonClass();
}
return $this->_instance;
}
private function __construct(){}
}
// @ main code
require_once ('path/to/singletonClass.inc');
$appl = singletonClass::getInstance();
Image you could write:
$appl = use singletonClass();
with the class:
class singletonClass{
public function __instance()
{
return self;
}
private function __construct(){}
}
so the keyword use and the magic function __instance would be introduced.
The use keyword would also trigger the __autoload function so that including isn't needed.
I think this would give more logic structure, is better to read and is uniformer so that
understandig the code is faster.
What do you think? Usefull suggestion?
Thanks,
Marc Cools
Belgium