PHP Reference Static Variables in Static Declaration


HammerTime
Member
Registered: 20.02.13 13:54
Timezone: UTC +0
Posts: 5

I'm having some issues figuring out how to do what I want to do here. I want to have a bunch of static variables containing arrays, and I want to be able to include one array inside another. But I always get an error.

Here's an example of what I'm trying:

<?php
class Test{
  static a = array('1'=>'a', '2'=>'b');
  static b = array('3'=>'c', '4'=>self::$a);
}
var_dump(Test::$b);
?>

And I just get the error message:

PHP Parse error:  syntax error, unexpected T_VARIABLE, expecting T_STRING in /var/www/test.php on line 4

Any ideas?


HammerTime
Member
Registered: 20.02.13 13:54
Timezone: UTC +0
Posts: 5

I'm having some issues figuring out how to do what I want to do here. I want to have a bunch of static variables containing arrays, and I want to be able to include one array inside another. But I always get an error.

Here's an example of what I'm trying:

<?php
class Test{
  static a = array('1'=>'a', '2'=>'b');
  static b = array('3'=>'c', '4'=>self::$a);
}
var_dump(Test::$b);
?>

And I just get the error message:

PHP Parse error:  syntax error, unexpected T_VARIABLE, expecting T_STRING in /var/www/test.php on line 4

Any ideas?


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 144

I don't think it's possible to do what you're trying to do in PHP. At least not in quite that same way. But maybe something like this would work:

class Test{
  static function getA(){
    return array('1'=>'a', '2'=>'b');
  }
  static function getB(){
    return array('3'=>'c', '4'=>self::getA());
  }
}

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 144

I don't think it's possible to do what you're trying to do in PHP. At least not in quite that same way. But maybe something like this would work:

class Test{
  static function getA(){
    return array('1'=>'a', '2'=>'b');
  }
  static function getB(){
    return array('3'=>'c', '4'=>self::getA());
  }
}

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


HammerTime
Member
Registered: 20.02.13 13:54
Timezone: UTC +0
Posts: 5

Awesome, that works good enough for what I'm doing. Thanks!


HammerTime
Member
Registered: 20.02.13 13:54
Timezone: UTC +0
Posts: 5

Awesome, that works good enough for what I'm doing. Thanks!


Senzon
Member
Registered: 25.05.12 19:13
Timezone: UTC +3
Posts: 17

I just noticed that the code I just submitted the other day in the code bank is maybe a good example of what ynori suggsted: Link


Senzon
Member
Registered: 25.05.12 19:13
Timezone: UTC +3
Posts: 17

I just noticed that the code I just submitted the other day in the code bank is maybe a good example of what ynori suggsted: Link


HammerTime
Member
Registered: 20.02.13 13:54
Timezone: UTC +0
Posts: 5

Senzon wrote:
I just noticed that the code I just submitted the other day in the code bank is maybe a good example of what ynori suggsted: Link

Yeah, I saw that. Actually I already commented on it too smile. Looks good.


HammerTime
Member
Registered: 20.02.13 13:54
Timezone: UTC +0
Posts: 5

Senzon wrote:
I just noticed that the code I just submitted the other day in the code bank is maybe a good example of what ynori suggsted: Link

Yeah, I saw that. Actually I already commented on it too smile. Looks good.