View Single Post
Old 05-08-2008, 08:52 PM   #6 (permalink)
Prpl_Ppl_Etr
The Visitor
 
Join Date: May 2008
Posts: 1
Thanks: 0
Prpl_Ppl_Etr is on a distinguished road
Default

I'm having some trouble getting my Singleton to hold state.

I have a class that implements the Singleton pattern and has a member called $sessionState which is meant to hold a string value denoting the current state of the Singleton instance.

Now, maybe it's my lack of understanding when it comes to sessions, but every time I retrieve an instance of my Singleton class, the $sessionState member gets reset.

Code fragments follow:

PHP Code:
class Control{
    
    private static 
$sessionState;
    
    private static 
$instance;
    
    
// private constructor
    
private function __construct(){
        
// initalize state value
        
self::$sessionState='Not Initialized';
    }
    
    
// the getInstance() method returns a single instance of the object
    
public static function getInstance(){
        if(empty(
self::$instance)){
            
self::$instance=new Control();
        }
        return 
self::$instance;
    }

    
    
    public function 
Initialize(){
        
$return// either 'true' or 'false'
        
switch(self::$sessionState){
            case 
'Not Initialized':
                
$monitor=SessionMonitor::getInstance();
                
$initialized=$monitor->initialize();
                if(
$initialized=='false'){
                    
$this->setErrorCode('102');
                    
$return='false';
                }else{
                    
self::$sessionState='Running';
                    
$this->setErrorCode('0');
                    
$return='true';
                }
            break;
            case 
'Running':
                
$this->setErrorCode('103');
                
$return='false';
            break;
            case 
'Terminated':
                
$this->setErrorCode('104');
                
$return='false';
            break;
            default:
                
$this->setErrorCode('101');
                
$return='false';
        }
        return 
$return;
    }

As you can see, I want the Initialize method to check the current value of $sessionState and respond accordingly. After a successful intialization of SessionMonitor, the $sessionState should be updated from 'Not Initialized' to 'Running' (which testing proves DOES happen). I'd expect, then, that any subsequent calls to other Control methods which depend on the value of $sessionState would read a value of 'Running'.

Such as...

PHP Code:
public function GetValue($cmi,$dbConn){
        
$return//either a string value or empty string
        
switch(self::$sessionState){
            case 
'Not Initialized':
                
$this->setErrorCode('122');
                
$return='';
            break;
            case 
'Running':
                
// get the type of model element
                
$cmiObject=$this->getCMIObject($cmi);
                
// 
                
$return=$cmiObject->getValue($dbConn);
                if(
$return==false){
                    
$error=$cmiObject->getError();
                    
$this->setErrorCode($error->errorCode);
                    
$return='';
                }else{
                    
$this->setErrorCode('0');
                }
            break;
            case 
'Terminated':
                
$this->setErrorCode('123');
                
$return='';
            break;
            default:
                
$this->setErrorCode('101');
                
$return='';
        }
        return 
$return;
    } 
If I call this method (after a successful call to Initialize), the case block for 'Running' doesn't execute, as the value for $sessionState seems to get reset to 'Not Initialized'.

What am I missing? Are Singleton instances cleared/collected with subsequent http requests to the same php file?
Prpl_Ppl_Etr is offline  
Reply With Quote