Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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();
        ...
        ...
        ...

you see what I mean.

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.

Am I correct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
799 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...