NickF
NickF

Reputation: 5747

PHP - singletone global instance

In my PHP project I created a singletone class that mange my access tokens.
The tokens are some constant strings with timestamp of +/- 5 min hashed with SHA1.
I want that each page can access to this singletone instance (the same instance) because it holds the tokens. Also there going to be a procedure that refresh the tokens in that instance.
The name TokenManager with located in different php file.
I create the instance $TOKENS in different file.

<?php

require_once 'TokenManager.php';
$TOKENS = TokenManager::Instance();

And another file for refresh action (refreshTokens.php):

<?php

require_once 'Tokens.php';
global $TOKENS;
$TOKENS->refreshTokens();
var_dump($TOKENS->tokens);

In another page that is a web service (AddUser) I use the this Tokens.php instance as global.

    require_once 'TokenManager.php';
    require_once 'Tokens.php';
...................................
    function start(){
        global $userParams;
        global $TOKENS;
        //Check for all mandatory params
        if(!ValidateParams()){
            finish();
        }

        if(!$TOKENS->checkTokenAndGetChannel($userParams[PARAM_TOKEN])){
            setError(ERR6_BAD_TOKEN, CODE6_DESC);
            finish();
        }

        if(!isEmailValidByDrupal($userParams[PARAM_EMAIL])){
            setError(ERR3_BAD_EMAIL, CODE3_DESC . $userParams[PARAM_EMAIL]);
            finish();
        }

        finish();
    }

The problem that each time I call refreshTokens.php and take the token I have each time new instance with a different values what makes the tokens each time invalid.

What can I do about that?

Upvotes: 1

Views: 404

Answers (1)

Steini
Steini

Reputation: 2783

Well when a called PHP Script ends all objects are destroyed and a singleton class will not really help you here.

A singleton class can be used as a cache or storage during the entire runtime of one page-call.

In other words, the singleton instance alone cannot keep the data between script calls.

Save them in the session, in a file or a database.

@Edit: After discussing the issue we came up it is the best to use class constants, I present you an example here:

@Edit2: Rethought the discussion and a Singleton class is fine if you do it correct, I leave you an example here:

final class Tokens {
    static private $instance = null;
    private $tokens = array();

    static public function getInstance() {
        if(self::$instance === null) {
            self::$instance = new self();
            self::$instance->initialize();
        }
        return self::$instance;
    }

    private function initialize() {
        $this->tokens[] = "erhdfhegrtoken1!";
        $this->tokens[] = "4h43gherhtoken2!";
        $this->tokens[] = "egegtoken3!";
    }

    public function getToken($index) {
        $retVal = "";

        if(isset($this->tokens[$index])) {
            $retVal = $this->tokens[$index];
        }

        return $retVal;
    }

    private function __construct() { }
    private function __clone() { }
}

Usage:

$tokens = Tokens::getInstance();
$myToken = $tokens->getToken(2);

Upvotes: 1

Related Questions