3

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!

7
  • Have you tried using the global keyword? Commented Aug 29, 2014 at 21:39
  • See this stackoverflow question Commented Aug 29, 2014 at 21:39
  • Exactly like you do in the constructor? By the way, you should not use global variables, just send the parameters you need to the method. Or perhaps you mean to use contants instead of variables? Commented Aug 29, 2014 at 21:39
  • This code is very bad in terms of architecture Commented Aug 29, 2014 at 21:39
  • You access global variables in static methods exactly the same way you access them in any other function. Either by using $GLOBALS or by using the global declaration. What's the problem? Commented Aug 29, 2014 at 21:42

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

self::$var1 is better, because when he decide to change a class name, then he'd have to rewrite all those foo::$var1 everywhere. Breaks DRY principle, therefore +0
This would depend on an object being instantiated at least once before you use the static method, otherwise the variables will not be initialized.
@jeroen: true enough, but there's no other way to do it. can't do it at the attribute definition,b ecause that'd be an expression and can't be resolved at compile time.
You could instantiate an object in the returnVars2 function to ensure that the constructor has run.
@barmar: that might work. never tried instantiating an "self" object inside the object's own constructor. seems canibalistic, somehow.
3

or much simple solutuion:

$mydataExample=array(1,2,3,4,5,6,7);

function myDataFunction() {
  global $mydataExample;
  return $mydataExample;
}

and then we can use this array in public static function:

public static function example() {
 $data = myDataFunction();
 print_r($data);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.