Each form has advantages and disadvantages.
Text file
At the time when I was developing a PHP framework, I thought a lot about the possibility of storing darlings in text files.
This would reinforce the separation of concepts, that is, I did not want to worry about the syntax of PHP mixed with SQL.
However, after considering the impact of read and process the files, I gave up the idea.
Separate PHP file
I used this approach in a project and found it very interesting. Each class had a respective PHP file with the darlings in strings. The heredoc
is an interesting representation in this approach.
The advantage is that you get an overview of darlings, facilitating SQL refactoring, and your code is cleaner. The downside is that sometimes you need to keep switching file several times to see the contents of the variables.
Queries inline
Today I avoid to the fullest, in any language put the darlings directly within a function or method.
First because with Sqls distributed in the middle of the code, if the table changes, I often forget to update one of the darlings "lost" in files and methods.
Second, because, although it actually works well for darlings very simple, in general ends up "spoiling" the formatting of the code and "breaking" the flow of it, disturbing its reading.
Representing SQL in String
To represent SQL in a PHP String, I would avoid as much as possible the form that concatenates line by line. This is asking for a tendinitis.
Using multi-line quotes, it doesn’t look so ugly if you don’t use tab. Example:
<?php
$sql = "
SELECT *
FROM usuarios u
ORDER BY u.nome";
The only String problem above is a line break at the beginning.
Already the format heredoc
seems to me the most interesting. First because there is no problem of line breaking or formatting. Moreover, it allows you to copy and paste the query easily, which I consider important, as I often do during development.
Once you get a little used to it it won’t seem so strange. ;)
On the other hand, as mentioned in the question itself, there is still the format Nowdoc
. According to the documentation, it works as simple quotes, where the content is not analyzed and thus, variables are not interpolated. The use of Nowdoc
is interesting because it avoids unnecessary processing in "constant" strings, that is, there are no PHP variables inside.
General considerations
Adopt a standard from which to store darlings facilitates the understanding of the system by developers and avoids the introduction of bugs, since there are no Sqls spread by the code.
Stick to objective criteria. Personal taste does not fit a platform of questions and answers. If there are many answers just exposing the taste, it will lead to the closure of the question because it is "only based on opinions". http://meta.pt.stackoverflow.com/questions/486/good-subjective-bad-subjective
– Maniero
Very good, I hadn’t seen that text. In fact the question is much more subjective than objective, and would tend to those discussions that "never end". Thank you!
– Rodrigo Rigotti