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
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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
Old 11-09-2010, 10:44 PM   #2 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Are you replacing the contents of $err->errorList anywhere between those two print_r()s? I ran your code exactly as you posted it and some error was showing twice.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 11-10-2010, 07:21 PM   #3 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
roman is on a distinguished road
Default

Hi Eric,

I don't change the $err content meanwhile, it is just empty when I try to print_r() it...
roman is offline  
Reply With Quote
Old 11-14-2010, 02:38 PM   #4 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 7
Thanks: 0
jgetner is on a distinguished road
Default

try this on for size..

PHP Code:

class Errors
{
   private 
$errorsList = array();

   private static 
$instance;

   private function 
__construct()
   {
   }
   
   public static function 
Instance()
   {
     if(!(
self::$instance instanceof self)
     {
        
self::$instance = new self;
     }

     return 
self::$instance;
   }

  public static function 
AddError($id $error)
  {
    if(!(isset(
self::$instance->errorsList[$id])
    {
       return 
self::$instance->errorsList[$id] = $error;
    }

    elseif(isset(
self::$instance->errorsList[$id])
    {
       return 
self::$instance->errorsList[$id] = $error;
    }

    else
    {
      throw new 
Exception('The Current Error Value Could Not Be Located');
    }

    return(
FALSE);
  }

  public static function 
GetError($id)
  {
    if(isset(
self::$instance->errorsList[$id])
    {
       return 
self::$instance->errorsList[$id];
    }
    
    else
    {
      return(
FALSE);
    }
  }
}

example....

$error Errors::Instance();
$errors->AddError('userError' 'Your Have An Error');

print_r($errors->GetError('userError')); 
jgetner is offline  
Reply With Quote
Reply



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 02:04 PM.

 
     

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