Skip to content

Worker mode question #466

Answered by withinboredom
KDederichs asked this question in Q&A
Discussion options

You must be logged in to vote

Essentially, all global/static variables are available from request to request. So, if you store information in static/global variables, your app will probably behave in unexpected ways.

However, sometimes you need static variables :). In that case, refactoring the static variable like so works perfectly:

class WrongWay {
    private static bool $var = false;

    public function __construct(RequestObject $request) {
        if(!self::$var) {
            self::$var = true;
            // perform per request initialization
            // this will be wrong on the next request because $var will be true!
        }
    }
}

class BetterWay {
    /**
     * @var WeakMap<bool>
     */
    private 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@KDederichs
Comment options

Answer selected by KDederichs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants