如何从PHP表达式中展开静态类变量?

如何从PHP表达式中展开静态类变量?,php,static-members,heredoc,Php,Static Members,Heredoc,我试图得到一个静态类变量,以在类构造函数中的表达式内部展开/解析,但我找不到一种方法使其工作。请参见下面我非常简化的示例: class foo { private static $staticVar = '{staticValue}'; public $heredocVar; public function __construct() { $this->heredocVar = <<<DELIM The value of the stat

我试图得到一个静态类变量,以在类构造函数中的表达式内部展开/解析,但我找不到一种方法使其工作。请参见下面我非常简化的示例:

class foo {

  private static $staticVar = '{staticValue}';

  public $heredocVar;

  public function __construct() {
    $this->heredocVar = <<<DELIM
    The value of the static variable should be expanded here: {self::$staticVar}
DELIM;
  }
}

// Now I try to see if it works...
$fooInstance = new foo;
echo $fooInstance->heredocVar;
此外,我还尝试了各种方法来引用静态变量,但运气不好。我正在运行PHP版本5.3.6


编辑

正如Thomas所指出的,可以使用实例变量来存储对静态变量的引用,然后在herdoc中使用该变量。下面的代码很难看,但确实有效:

class foo {

  private static $staticVar = '{staticValue}';

  // used to store a reference to $staticVar
  private $refStaticVar;

  public $heredocVar;

  public function __construct() {
    //get the reference into our helper instance variable
    $this->refStaticVar = self::$staticVar;

    //replace {self::$staticVar} with our new instance variable
    $this->heredocVar = <<<DELIM
    The value of the static variable should be expanded here: $this->refStaticVar
DELIM;
  }
}

// Now we'll see the value '{staticValue}'
$fooInstance = new foo;
echo $fooInstance->heredocVar;
class-foo{
私有静态$staticVar='{staticValue}';
//用于存储对$staticVar的引用
私有$refStaticVar;
公共资本:埃雷多瓦尔;
公共函数构造(){
//将引用获取到helper实例变量中
$this->refStaticVar=self::$staticVar;
//用我们的新实例变量替换{self::$staticVar}
$this->heredocVar=怎么办


我会设置
$myVar=self::$staticVar;
,然后在herdoc代码中使用
$myVar

没有人声称它很漂亮;-)我会说它并不比使用
私有静态
类属性更粗糙:)
class foo {

  private static $staticVar = '{staticValue}';

  // used to store a reference to $staticVar
  private $refStaticVar;

  public $heredocVar;

  public function __construct() {
    //get the reference into our helper instance variable
    $this->refStaticVar = self::$staticVar;

    //replace {self::$staticVar} with our new instance variable
    $this->heredocVar = <<<DELIM
    The value of the static variable should be expanded here: $this->refStaticVar
DELIM;
  }
}

// Now we'll see the value '{staticValue}'
$fooInstance = new foo;
echo $fooInstance->heredocVar;