TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 11-05-2010, 03:02 PM   #1 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
roman is on a distinguished road
Default Public variable in singleton do not survive

Hi,

I am trying to use singleton in my application. I believed singleton object can hold public variables to be used at any place of my code. But it does not work... Within the same request the singleton loses public vars content. Thanks to anyone who can help me :)


PHP Code:

// singleton to keep errors
class Errors {
    
    private static 
$_instance;
    public 
$errorList;
    
    private function 
__construct () {
        
$this->errorList = array();
    }
    
    public static function 
getInstance() {
        if (empty(
self::$_instance))
            
self::$_instance = new Errors ();
        return 
self::$_instance;
    }
    
    public function 
addError ($error) {
        
$this->errorList[] = $error;
    }                
}

// when I use the singleton first time, it holds the public value of errorList
$err Errors::getInstance();
$err->addError('some error');
print_r ($err->errorList);

// ... some code here

// when I get the instance again within the same request, the errorList seems to have lost it's value ('some error')
$err Errors::getInstance();
print_r ($err->errorList); 
roman is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Singleton in Session variable? svdelle Absolute Beginners 6 10-07-2009 02:19 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 10:31 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design