php - HEREDOC 干扰代码缩进

标签 php heredoc

我喜欢 HEREDOC 语法,例如对于不值得放入模板的生成 HTML 的边缘情况。

不过,唯一让我恼火的是内容和 heredoc 字符串的结束标记紧贴第一列。这搞砸了嵌套代码布局:

class myclass 
 { 

    function __construct()
      { 
       $a = some_code();
       $b = some_more_code();
       $x = <<<EOT

line1
line2
line3
line4

EOT;    

        $c = even_more_code();
        $b = still_more_code();
        ...
        ...
        ...

你明白我的意思了。

现在这可能无法使用普通的 HEREDOC 解决。有人解决过这个问题吗?我的梦想是拥有带自动缩进的 HEREDOC 语法。但我想如果不为源文件编写一些预编译器,这是不可能的。

我说的对吗?

最佳答案

谢天谢地,此功能终于通过 RFC 登陆 php 7.3:Flexible Heredoc and Nowdoc Syntaxes

现在您的示例可以清晰地写为:

class myclass
{
    function __construct()
    {
        $a = some_code();
        $b = some_more_code();
        $x = <<<EOT

        line1
        line2
        line3
        line4

        EOT;

        $c = even_more_code();
        $b = still_more_code();
    }
}

关于php - HEREDOC 干扰代码缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2305869/

相关文章:

ruby 这里的文件

python - 将此处文档与 pxssh (pexpect) 一起使用

python - 为什么 "End of File"(EOF) 是此处文档的实际分隔符?

php - HTML 到经典 ASP - 页面加载为代码行 (ASP)

php - 内存/时间高效的数据库输入

php - Freebase API 调用在浏览器中有效,但因 curl 而失败

当超过 512 个字符时,bash heredoc 挂起

javascript - 将选择元素设置为在表单提交时禁用(JQuery)

php - Docker mysql无法连接到容器

linux - "cat << EOF"如何在 bash 中工作?