singleton problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I was trying to knock up an example of a singleton, but in the following 
example it doesn't print the correct output.  Can you spot the problem?

Ta,
Peter

<?php

Class Display
{
     var $text = 'Default';
     function Display()
     {
         return;
     }

     function setText($text)
     {
         $this->text = $text;
     }

     function getText()
     {
         return $this->text;
     }
}

Class UsefulStuff
{
     function singleton($class)
     {
         if (!$GLOBALS['__useful_stuff'][$class]) {
             // Add including the class if not done elsewhere
             $GLOBALS['__useful_stuff'][$class] = new $class();
         }
         return $GLOBALS['__useful_stuff'][$class];
     }
}

Class Client
{
     var $display = null;

     function Client()
     {
         $this->display =& UsefulStuff::singleton('Display');
     }
     function changeName($n)
     {
         $this->display->setText($n);
     }
}

####################
$d =& UsefulStuff::singleton('Display');
$c =& new Client;
print $d->getText() . "\n"; // Should print Default
$c->changeName('Peter');
print $d->getText() . "\n"; // Should print Peter

var_dump($d);
var_dump($c);

?>

-- 
Maple Design - quality web design and programming
http://www.mapledesign.co.uk  



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

PHP Data object relational mapping generator - http://www.meta-language.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux