Php 在静态变量定义中访问静态方法

Php 在静态变量定义中访问静态方法,php,class,static-methods,static-variables,Php,Class,Static Methods,Static Variables,我试图访问静态类变量定义中的静态类方法。我尝试了几次,但都无法编译代码 天真的尝试: 直观的尝试: 逻辑尝试: 绝望的尝试: 任务似乎很简单,使用静态方法定义静态变量,但我不知道如何在静态变量声明的上下文中正确访问静态方法 如何在PHP v5.5.3的静态变量定义中调用静态方法?正确的尝试: 就在类声明之后,或者当您想要实例化它时 证明 与任何其他PHP静态变量一样,静态属性只能使用文本或常量初始化;不允许使用表达式。因此,虽然可以将静态属性初始化为整数或数组(例如),但不能将其初始化为

我试图访问静态类变量定义中的静态类方法。我尝试了几次,但都无法编译代码

天真的尝试:
直观的尝试:
逻辑尝试:
绝望的尝试:
任务似乎很简单,使用静态方法定义静态变量,但我不知道如何在静态变量声明的上下文中正确访问静态方法

如何在PHP v5.5.3的静态变量定义中调用静态方法?

正确的尝试: 就在类声明之后,或者当您想要实例化它时

证明

与任何其他PHP静态变量一样,静态属性只能使用文本或常量初始化;不允许使用表达式。因此,虽然可以将静态属性初始化为整数或数组(例如),但不能将其初始化为其他变量、函数返回值或对象


你能引用“仅标量和数组”规范中的PHP文档吗?@awashburn,真的?!我想你的PHP解释器向你展示了这一点。你认为为什么会出现这些错误?--更新的答案。感谢您快速、正确、引用且格式良好的答案:)@awashburn如果您想知道原因,主要是因为静态在解析时初始化,但表达式可能依赖于外部内容,因此要求您选择何时对静态进行初始化。这很有意义,我觉得PHP的解释器没有那么健壮。
<?php
class TestClass {
  private static $VAR = doSomething(array());

  private static function doSomething($input) {
    return null;
  }
}
?>
dev@box:~/php$ php -l TestClass.php 
PHP Parse error:  syntax error, unexpected '(', expecting ',' or ';' in TestClass.php on line 3
Errors parsing TestClass.php
<?php
class TestClass {
  private static $VAR = self::doSomething(array());

  private static function doSomething($input) {
    return null;
  }
}
?>
dev@box:~/php$ php -l TestClass.php 
PHP Parse error:  syntax error, unexpected '(', expecting ',' or ';' in TestClass.php on line 3
Errors parsing TestClass.php
<?php
class TestClass {
  private static $VAR = static::doSomething(array());

  private static function doSomething($input) {
    return null;
  }
}
?>
dev@box:~/php$ php -l TestClass.php
PHP Fatal error:  "static::" is not allowed in compile-time constants in TestClass.php on line 3
Errors parsing TestClass.php
<?php
class TestClass {
  private static $VAR = $this->doSomething(array());

  private static function doSomething($input) {
    return null;
  }
}
?>
dev@Dev08:~/php$ php -l TestClass.php 
PHP Parse error:  syntax error, unexpected '$this' (T_VARIABLE) in TestClass.php on line 3
Errors parsing TestClass.php
<?php
class TestClass {
  private static $VAR = null;

  private static function doSomething($input) {
    return null;
  }

  public static function Construct(){
      self::$VAR = self::doSomething(array());
  }
}
TestClass::Construct();
?>
TestClass::$VAR = TestClass::doSomething(array());