使用静态类变量 - 在heredoc中

wan*_*est 10 html php

我设置了一个类,简化了这个:

class Labels {
    static public $NAMELABEL = "Name";
}
Run Code Online (Sandbox Code Playgroud)

我成功地得到以下代码才能正常工作:

echo '<table border="1">';
  echo '<tr>';
  echo "<th>" . Labels::$NAMELABEL . "</th>";
  echo '</tr>';

 // the rest of the Table code not shown for brevity...

echo "</table>";
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我看到一个带有名为Name的列标题的表- 所以它工作正常.

但不是在heredoc中 - 当我运行以下内容时,我得到"注意:未定义的变量:NAMELABEL在C:\ xampp ........ blah blah":

    echo <<<_END
       <form action="index.php" method="post"><pre>
       Labels::$NAMELABEL : <input type="text" name="author" />
       <input type="submit" value="ADD RECORD" />
    </pre></form>
_END;
Run Code Online (Sandbox Code Playgroud)

我已经尝试了各种引用,字符串连接运算符'.',没有任何作用.我想"我有静态类变量在HTML表中工作,为什么不是 heredoc."

我喜欢heredocs,他们有一个奇怪的名字和奇怪的问题.这是我渴望的那种令人心碎的乐趣,heredocs是正义的小doosh猴子.

我真的想在这里使用我的静态类变量 - 引用/字符串连接的某些组合是否允许我将它们嵌入到我的heredocs中?

Jor*_*ing 9

heredocs中的插值与双引号中的插值相同,因此您可以使用大括号("复杂")语法.

但是,解析器无法识别静态类变量(请参阅前面的文档).为了引用静态类变量,您需要在本地设置它们,如下所示:

$label = Labels::$NAMELABEL;

echo <<<_END
    <form action="index.php" method="post"><pre>
       $label : <input type="text" name="author" />
       <input type="submit" value="ADD RECORD" />
    </pre></form>
_END;
Run Code Online (Sandbox Code Playgroud)