HEREDOC干扰代码缩进 [英] HEREDOC interfering with code indentation

查看:90
本文介绍了HEREDOC干扰代码缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢HEREDOC语法,例如对于生成的HTML的极端情况,这些情况不值得放在模板中.

I like the HEREDOC syntax, e.g. for edge cases of generated HTML that are not worth putting into a template.

但是,唯一令我烦恼的是,内容和Heredoc字符串的结束标记粘附在第一列上.这会弄乱嵌套的代码布局:

The only thing that annoys me about it, though, is that the content, and the closing marker of a heredoc string adheres to the first column. This screws up nested code layouts:

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语法.但是我想,如果不为源文件编写一些预编译器,这是不可能的.

Now this is probably not solvable using normal HEREDOC. Has anybody worked around this? My dream would be to have HEREDOC syntax with automatic indentation. But I guess this is not possible without writing some pre-compiler for the source files.

我正确吗?

推荐答案

此功能终于通过RFC通过php 7.3登陆:灵活的Heredoc和Nowdoc语法

Thank goodness this feature has finally landed in php 7.3 via RFC: Flexible Heredoc and Nowdoc Syntaxes

因此,您的示例现在可以清楚地写为:

So now your example can cleanly be written as:

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干扰代码缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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