使用静态类变量 - 在heredoc中 [英] Using static class variables -- in a heredoc

查看:102
本文介绍了使用静态类变量 - 在heredoc中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  class Labels {
static public $ NAMELABEL = 名称;
}

我成功获得了以下代码:

  echo'< table border =1>'; 
echo'< tr>';
echoth 。 Labels :: $ NAMELABEL。 < /第> 中;
echo'< / tr>';

//为简洁起见,表格代码的其余部分未显示...

echo< / table>;

运行此操作时,我看到一个名为 Name 的列标题表 - 所以它工作正常。



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

  echo<<< _END 
< form action =index.phpmethod =post>< pre>
Labels :: $ NAMELABEL:< input type =textname =author/>
< input type =submitvalue =ADD RECORD/>
< / pre>< / form>
_END;

我试过各种各样的引用,字符串concat运算符'。',没有任何作用。
我想我得到的静态类变量在HTML表格中工作,
为什么不是 heredoc。



当我喜欢heredocs时,他们带着一个奇怪的名字和奇怪的问题。
这是我渴望的那种令人心动的乐趣,heredocs是
正义的小猴子。



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

解决方案

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

然而解析器不能识别静态类变量文档)。为了引用静态类变量,您需要在本地设置它们,如下所示:

  $ label = Labels :: $ NAMELABEL; 

echo<<< _END
< form action =index.phpmethod =post>< pre>
$ label:< input type =textname =author/>
< input type =submitvalue =ADD RECORD/>
< / pre>< / form>
_END;


I set up a class, which simplified is this:

class Labels {
    static public $NAMELABEL = "Name";
}

I successfully got the following code to work fine:

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>";

I see a table with a column header called Name when I run this -- so it works fine.

But not inside a heredoc -- I get "Notice: Undefined variable: NAMELABEL in C:\xampp........blah blah" when I run the following:

    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;

I've tried all sorts of quoting, string concat operator '.', nothing works. I figured "Well I got the static class variables to work in an HTML table, why not a heredoc."

Dang I love heredocs, they come with a weird name and weird problems. It's the sort of mind-bending kind of fun I crave, heredocs are righteous little doosh monkeys.

I really want to use my static class variables here -- is there some combination of quoting/string concatenation that will allow me to embed them into my heredocs?

解决方案

Interpolation in heredocs works the same as in double quotes, so you can use curly brace ("complex") syntax.

However the parser does not recognize static class variables (see previous documentation). In order to refer to static class variables, you will need to set them locally as follows:

$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;

这篇关于使用静态类变量 - 在heredoc中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆