HEREDOC interfering with code indentation
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();
}
}
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.
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
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 --)