Q:

Access Global Variables within a Static Method in PHP

I have some php code that works similar to this:
$var1 = 'Variable 1';
$var2 = 'Variable 2';

class myClass {

    function __construct() {
        $this->var1 = $GLOBALS['var1'];
        $this->var2 = $GLOBALS['var2'];
    }

    public function returnVars() {
        return $this->var1 . ' ' . $this->var2; //Works fine
    }

    public static function returnVars2() {
        //What should I do here?
    }

}

If I instantiate this class using $myClass = new myClass(), I can use the returnVars() method or just get var1 and var2 from the class just fine. My problem is I need to use the global variables $var1 and $var2 within the static method returnVars2(). I can't use this $this keyword because the class isn't instantiated (and they get declared inside the constructor), but I still can't access the global variables normally.
Any suggestions are greatly appreciated!  

A:

Declare the variables as static members:
class foo {
    public static $var1;
    public static $var2;

    function __construct() {
       foo::$var1 = $GLOBALS['var1'];
       foo::$var2 = $GLOBALS['var2'];
    }
}

Then you can access them as foo::$varX in any method within that class, static or not.

