-1

This won't work:

class Someclass {
   public static $v = '28';
   private static $a = Array (
      'theNumber' => self::$v
   );
}

it yields:

PHP Parse error: syntax error, unexpected '$v' (T_VARIABLE), expecting identifier (T_STRING) or class (T_CLASS)

How can I have theNumber use a static member of Someclass as a value?

EDIT: please read the question before answering or "marking as duplicate", this question is not about syntax. It is whether or not it's possible to use a static var in an array (which isn't).

Yuval A.
  • 5,849
  • 11
  • 51
  • 63
  • What version of PHP are you on? – Halcyon Apr 30 '15 at 14:29
  • not the answer, but you should put "theNumber" in quotes – I wrestled a bear once. Apr 30 '15 at 14:34
  • I'm using version 5.5 – Yuval A. Apr 30 '15 at 14:34
  • (It's with quotes now) – Yuval A. Apr 30 '15 at 14:35
  • @John Conde - I don't see any relation to the question you marked as a duplicate for this question. My question was how it is possible to use a static var as a value of an array, it's not related to syntax (even though I quoted the syntax error) but to functionality. Anyway I now understand that only allowing to use a constant there makes sense, functionality-wise (since the array is initialized when the class is initialized). The solution to the question is to simply add a function to the class, that changes that specific property of the array. – Yuval A. Apr 30 '15 at 18:53

1 Answers1

2

Just use a constant:

class Someclass {
   const NUM = '28';
   public static $v = NUM;
   private static $a = Array (
      'theNumber' => NUM
   );
}
laurent
  • 88,262
  • 77
  • 290
  • 428
  • What if I want that value to be changeable when using the class? – Yuval A. Apr 30 '15 at 14:33
  • @YuvalA., you cannot change a const but you can change the two static values. If you mean you want both static values to change automatically when you change a third variable, then you cannot do this either. – laurent Apr 30 '15 at 15:44