azaryc2s azaryc2s - 1 year ago 100
PHP Question

PHP: Reference another static variable inside a static variable

Either I'm too dumb or this is somehow not possible in (which is really a basic functionality in any programming language...):
So here's the example for my problem:

class Test {
private static $A = "test";
private static $B = "This is a " . Test::$A . " to see if it works";

My expected result is the variable
to have the value =
This is a test to see if it works

But somehow I'm getting this error:

Parse error: syntax error, unexpected '$A' (T_VARIABLE), expecting identifier (T_STRING) or class (T_CLASS) in /.../class.Test.php on line 4

Is this something is unable to do or is it just some stupid typo? I'm unable to find the error since like an hour...

Thanks in advance

Answer Source

Another solution if you don't want to have another clas

class TestStatic
    private static $A = 'test';
    private static $B;

    //if you want to instantiate the object
    public function __construct() {

    //if you don't want to instantiate the class 
    public static function getB() {
       return self::$B;

    private static function setB() {
       if (!isset(self::$B)) {
        self::$B = 'This is a '.self::$A.' to see if it works';


echo TestStatic::getB();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download