View Single Post
Old 01-06-2009, 02:01 AM   #8 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Do you want to use the singleton on an individual basis like the following? Do you really want a factory, as I showed you above?

php Code:
class TalkPHP_Hello
{
    public static $m_pInstance;
   
    public static function getInstance()
    {
        if (!isset(self::$m_pInstance))
        {
            self::$m_pInstance = new self();
        }
       
        return self::$m_pInstance;
    }
   
    public function getText()
    {
        return 'Hello TalkPHP.com';
    }
}

echo TalkPHP_Hello::getInstance()->getText();

This is the best way for it to be application wide. You just call getInstance() and there you have the instance. If you don't need many instances of the object, simply use it like so.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Scottymeuk (01-07-2009)