TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Public variable in singleton do not survive (http://www.talkphp.com/advanced-php-programming/5623-public-variable-singleton-do-not-survive.html)

roman 11-05-2010 03:02 PM

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); 


wGEric 11-09-2010 10:44 PM

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.

roman 11-10-2010 07:21 PM

Hi Eric,

I don't change the $err content meanwhile, it is just empty when I try to print_r() it...

jgetner 11-14-2010 02:38 PM

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')); 



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

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0