クラス コンストラクター内のHEREDOC式内で展開/解決する静的クラス変数を取得しようとしていますが、それを機能させる方法が見つかりません。以下の非常に単純化された例を参照してください。
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;
次の出力が得られます。
The value of the static variable should be expanded here: {self::}
さらに、静的変数を参照するためにさまざまな方法を試しましたが、うまくいきませんでした。PHP バージョン 5.3.6 を実行しています。
編集
Thomas が指摘したように、インスタンス変数を使用して静的変数への参照を格納し、その後HEREDOC 内でその変数を使用することができます。次のコードは醜いですが、動作します:
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;