View Single Post
Old 01-07-2009, 02:13 AM   #14 (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

That doesn't need to be static as it is called after the getInstance() call. Therefore it is now an object, as it were.

Nonetheless, a very good post!

Another way to do this, although I don't approve of this way, is to use $GLOBALS. I've wrapped it in two functions just to make it look slightly more elegant, but not a lot!

php Code:
class TalkPHP_Hello
{
    public function getText()
    {
        return 'Hello TalkPHP.com';
    }
}

class TalkPHP_Hello_Child
{
    public function getText()
    {
        /* The above class is now accessible from everywhere! */
        return getGlobal('pHello')->getText();
    }
}

function getGlobal($szVariable)
{
    return $GLOBALS[$szVariable];
}

function setGlobal($szVariable, $pObject)
{
    $GLOBALS[$szVariable] = $pObject;
}

setGlobal('pHello', new TalkPHP_Hello());
setGlobal('TalkPHP_Hello_Child', new TalkPHP_Hello_Child());

echo getGlobal('TalkPHP_Hello_Child')->getText();
__________________
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)