开发者

Using static class variables -- in a heredoc

开发者 https://www.devze.com 2023-03-11 05:39 出处:网络
I set up a class, which simplified is this: class Labels { static public $NAMELABEL = \"Name\"; } I successfully got the following code to work fine:

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

 /开发者_如何学JAVA/ 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号