HEREDOC interfering with code indentation

后端 未结 6 1073
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 10:48

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

相关标签:
6条回答
  • 2020-12-03 11:01

    FYI, this does not solve your problem, but Ruby programming language supports indented heredocs.

    You might want to send a feature request to the PHP devs, and use the Ruby docs as an example for proof that an existing language solves this problem, so why not PHP too?

    0 讨论(0)
  • 2020-12-03 11:09

    I wrote a function that allows you to indent as you wish. It's actually a pretty simple function. https://github.com/chiedolabs/moon-walk-php

    I like it because my code stays clean this way.

    0 讨论(0)
  • 2020-12-03 11:13

    That's a problem I often have too : the code is not well indented when I use heredoc, and I really like heredoc :-(

    A "bigger" problem is when you select a whole block of code, press "tab" (or any equivalent in your IDE) to indent it more because you added a condition arround it or anything... And it breaks the heredoc strings : you have to go un-indent them by hand :-(

    Unfortunatly, I've never seen any tool like the one you're describing...


    A solution, I suppose, would be to put the heredoc string in another file, and include it -- the include like could be indented normally ; but it would also mean one more file to load, which would make the code less clear.

    0 讨论(0)
  • 2020-12-03 11:14

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-03 11:18

    You cannot ident heredocs or nowdocs in PHP. This is my workaround:

    function foo() {
        $a = 123;
        $b = 456;
        $sum = $a + $b;
        $html = "
           <div>
             <h1>sum a, b</h1>
             Number a is $a, number b is $b<br>
             a+b equals <b>$sum<b>
           </div>
        ";
        echo $html;
    }
    

    This adds spaces to the generated html code but if you use mod_pagespeed or similar apache mods, your server will remove all unnecesary spaces.

    You can use the same technique for multi-row sql queries:

    function bar($sql, $id) {
        $q= "
           SELECT
             name
           , address
           , phone
           FROM users
           WHERE id = '$id' -- possible mysql inyection
           LIMIT 1
        ";
        $sql->query($q);
    }
    

    The code gains in readability. It has no impact in performance and you can comment compex SQL queries (with # or --)

    0 讨论(0)
  • 2020-12-03 11:23

    I just discovered an odd workaround for anyone still wondering how to do this. Indent your first line that starts the HEREDOC. Your second line, which is the first line of the HEREDOC, has to have no whitespace, so leave it as a blank line. Start a new line after that, indent it and write your code. Then complete the HEREDOC, again with no white space. Visually you'll get all of your code indented except for the completion of the HEREDOC. Highlight + TAB still an issue, but at least the code is more readable within control loops, etc, now.

               $html = <<< HTML                    //indented line
                                                   //leave this line empty
               <div>                               //indented line
                    <div>$variable</div>           //indented line
               </div>                              //indented line
    HTML;                                          //no white space, not indented
    
    0 讨论(0)
提交回复
热议问题