Re: One-line heredoc for better syntax highlightning

php.internals

Olle Härstedt

3 years ago
> I don't find that example particularly readable or convincing. It mushes > together PHP and MySQL keywords too much. > > Once you add a WHERE clause that SQL query should be split across > multiple multiple lines for readability anyway and even for that very > simple example I personally would add a linebreak in front of the FROM: > > $query = <<<MySQL > SELECT * > FROM foo > MySQL; > > Best regards >Tim Düsterhus
That looks good in isolation, but consider the following short snippet: class Foo { public function bar() { $query = <<<MySQL SELECT * FROM baz MySQL; } } As you can see, the heredoc breaks the reading flow completely. For smaller SQL queries, using one line would be an improvement, I think. Also consider that the one line will look better in your color highlight editor. :) Olle

Tim Düsterhus

3 years ago
Hi On 9/19/22 21:04, Olle Härstedt wrote:
> That looks good in isolation, but consider the following short snippet: > > class Foo > { > public function bar() > { > $query = <<<MySQL > SELECT * FROM baz > MySQL; > } > } > > As you can see, the heredoc breaks the reading flow completely. For
The closing marker of the heredoc may be indented as of PHP 7.3, so the appropriate comparison would be this: <?php class Foo { public function __construct( private \PDO $dbConnection ) {} public function bar() { $query = <<<MySQL SELECT * FROM baz MySQL; $statement = $this->dbConnection->prepare($query); $statement->execute(); } }
> smaller SQL queries, using one line would be an improvement, I think.
A condition-less SELECT should be rare, a condition-less UPDATE or DELETE even rarer. So this "improvement" would only affect a very small number of statements in the first place and I disagree with that being an improvement even for that small number of statements, because it would be inconsistent with all the other larger statements which *are* spread across multiple lines.
> Also consider that the one line will look better in your color > highlight editor. :) >
I believe that code should be readable no matter the editor it is viewed in. The suggestion fails at that. Best regards Tim Düsterhus

Olle Härstedt

3 years ago
2022-09-19 21:18 GMT+02:00, Tim Düsterhus <tim@bastelstu.be>:
> Hi > > On 9/19/22 21:04, Olle Härstedt wrote: >> That looks good in isolation, but consider the following short snippet: >> >> class Foo >> { >> public function bar() >> { >> $query = <<<MySQL >> SELECT * FROM baz >> MySQL; >> } >> } >> >> As you can see, the heredoc breaks the reading flow completely. For > > The closing marker of the heredoc may be indented as of PHP 7.3, so the > appropriate comparison would be this: > > <?php > > class Foo > { > public function __construct( > private \PDO $dbConnection > ) {} > > public function bar() > { > $query = <<<MySQL > SELECT * > FROM baz > MySQL; > $statement = $this->dbConnection->prepare($query); > $statement->execute(); > } > }
Pff, that's the second time today 3v4l gives me complete faulty results in its syntax checker. Guess it's out of date. Thanks for correcting me here. Olle