Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use a variable in an array within my class

class MyClass {

public static $a = "World";

public static $b = array("Hello" => self::$a);

}

This code doesn't work. Should I be using MyClass::$a or something else. Any ideas?

share|improve this question
4  
Class properties can't contain expressions that have to be evaluated at runtime, and self::$a requires a runtime evaluation. If your $a isn't going to change, make it a constant –  Mark Baker Dec 29 '13 at 12:14
    
Initialize $b in the class constructor. –  Barmar Dec 29 '13 at 12:16
    
@Barmar, slightly awkward to initialize in the constructor as it's static –  Mark Baker Dec 29 '13 at 12:17
1  
Only slightly. if (!$b) { self::$b = array(...) }. –  Barmar Dec 29 '13 at 12:20
    
@Barmar - but that means instantiating the class, which shouldn't be a prerequisite for anything defined as static –  Mark Baker Dec 29 '13 at 12:22

3 Answers 3

up vote 3 down vote accepted

You probably can instatiate them at runtime:

class MyClass {
    public static $a;
    public static $b;
}

MyClass::$a = "World";
MyClass::$b = [ "Hello" => MyClass::$a ];

Or you can create a static initialization method:

class MyClass {
    public static $a;
    public static $b;

    public static function init(){
        static::$a = "World";
        static::$b = [ "Hello" => static::$a ];
    }
}

MyClass::init();
share|improve this answer
    
This is very good. It works on what @awlad-liton said, but this approach is more useful - thanks a lot! –  Jason Lipo Dec 29 '13 at 13:10
    
@JasonLipo glad to help. Good luck. –  HAL9000 Dec 29 '13 at 13:10

If your $a won't change, make it a constant

class MyClass {

    const a = "World";

    public static $b = array("Hello" => self::a);

}

var_dump(MyClass::$b);
share|improve this answer
    
Yes, this does work. However in the context I want to use it, I don't want $a to be a constant. –  Jason Lipo Dec 29 '13 at 12:36
    
In that case, if your properties still need to be static, @HAL9000 has provided your options. –  Mark Baker Dec 29 '13 at 12:37
class MyClass {

    public static $a = "World";
    public static $b;
    public function __construct(){
      self::$b = array("Hello" => self::$a);
    }

}
$obj = new MyClass();
share|improve this answer
    
Yes I think that's the answer. I have to use a __construct function. Thanks a lot! –  Jason Lipo Dec 29 '13 at 12:37
    
Note that if you want $b to automatically reflect any changes made to $a, you'll need to set self::$a by reference –  Mark Baker Dec 29 '13 at 13:06
    
@Jason Lipo: You accepted my answer and after that someone edited his answer after acceptance you did change your acceptance also!! –  Awlad Liton Dec 29 '13 at 13:19
    
I changed the acceptance not because of the edit. I tried the static function approach from @HAL9000 and that proved to work the most simply. –  Jason Lipo Dec 29 '13 at 21:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.