Req #66303 [Opn->Wfx]: Inline Heredoc / Nowdoc
| From: | aharvey@php.net | Date: | Tue, 17 Dec 2013 22:52:43 +0000 |
| Subject: | Req #66303 [Opn->Wfx]: Inline Heredoc / Nowdoc | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-183379@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66303&edit=1
ID: 66303
Updated by: aharvey@php.net
Reported by: childerkc at gmail dot com
Summary: Inline Heredoc / Nowdoc
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: *General Issues
Operating System: CentOS Linux
PHP Version: 5.5.7
Block user comment: N
Private report: N
New Comment:
Allowing the end token to appear as part of a larger line would make it far easier for heredocs to
be ended accidentally by the end token appearing in the heredoc content, the
<<<>>> option is veering very close to symbol soup, and since single and double
quotes can span multiple lines, I don't think there's much benefit here.
Previous Comments:
------------------------------------------------------------------------
[2013-12-17 22:47:50] childerkc at gmail dot com
And now that I re-look at my example, I didn't even put use of both " and ' inside
the heredoc example code, so imagine some single quotes in there too.. So those that come here and
feel the need to comment that, well I was tired.. if you understand how and why to use a heredoc,
well I am glad, we are on the same page, I simply would like to see them no longer forced to a
single line to start and end one, why can these not be [inline] with the rest of the code?
------------------------------------------------------------------------
[2013-12-17 22:40:58] childerk at gmail dot com
Ok ok you got me there, maybe I went a little overboard with my example, but it was simply to show
that if you have many of them, the ugliness accumulates.
My real main issue, is when done with some chunk of data that obviously has use of "" and
'' inside it, heredoc is a brilliant solution, as I don't need to backslash every
little thing, and where I use heredoc's are not to get large chunks of data in, but smaller
pieces, and so my documents, in cases where I use this, look ridiculous. I don't understand why
it is done this way in the first place? Nothing else in PHP, AFAIK, forces its syntax to be on a new
line, why would a heredoc? an if(could){$be_Done_This_Way;}, in a single line, why not a heredoc? :/
------------------------------------------------------------------------
[2013-12-17 02:20:54] mail at requinix dot net
Inline heredoc and nowdoc... So you mean normal string syntax? Surely
'#someID{width: 100px; height: 100px}'
is "sexier" than
<<<'CSS'>#someID{width: 100px; height: 100px}<CSS>>>
------------------------------------------------------------------------
[2013-12-17 01:29:00] childerkc at gmail dot com
Description:
------------
I use heredoc and nowdoc at times, and feel they are very powerful if you know why your using them.
With that, I wish they were sexier when it came to the syntax, I am not a fan of commands to go in
and out of a heredoc, being on their own line. This tends to get annoying if you are trying to pass
a bunch of different pieces of data to different parameters of a function. An example could be found
in the test script below
Test script:
---------------
// Obviously not the most logical function in this case, but point still holds true, heredocs are
ugly and don't need to be...
function foo($css,$js,$html) {handleCSS($b);handleJS($r);return $html;}
if($someTrueCondition) {
echo foo(
<<<'CSS'
#someID{width: 100px; height: 100px}
CSS
, // <---------- :(
<<<'JS'
alert("simple alert");
JS
, // <---------- :'(
<<<'HTML'
<div id="someID">$SomeOtherInnerHTML</div>
HTML
);
}
// The above code just makes me want to cry... such lonely random commas...
// Why instead, can this not be simplified? I have seen some other bug requests that just make the
heredocs more complicated.. my goal is to not make them any more complicated, just to simplify the
usage of them for us developers who do use them [maybe more then we should]... My Proposal is to
simply allow them to be inline like the rest of the code? And instead, maybe simply allow them
follow the flow of the developers ohh so beautiful code.. so..
// The only thing one would need to do is to instead make sure that the start of a heredoc is at the
END OF A LINE, and the END of a heredoc, is at the START of a line, EXCLUDING ALL DATA BEFORE START
TAG AND AFTER END TAG... So the above ugliness, becomes a little easier on the eyes:
if($someTrueCondition) {
echo foo(<<<'CSS'
#someID{width: 100px; height: 100px}
CSS, <<<'JS'
alert("simple alert");
JS, <<<'HTML'
<div id="someID">$SomeOtherInnerHTML</div>
HTML);
}
// And if an ending heredoc starts on a line, then breaks out of heredoc, it can then go back in to
it, on the same line, at the END of the line... I would also say that the heredoc should then handle
removing any white space in the content for each line that is equivalent to the whitespace before
the END tag for that heredoc... This would keep the code beautiful and the end result beautiful..
(maybe even leave the content alone, white space and all, no worries for me as something cleans if
up for me later anyway)..
// What I would LOVE to see, is the heredoc in an actual INLINE way... So that the above 2
ugly's become the sexy code below... This would mean the syntax would probably need to change a
little, start tag would be something simple like <<<'EOD'> and the end tag
would be the opposite without the quotes <EOD>>>, everything between the > and <
would adhere to the heredoc standards...
if($someTrueCondition) {
echo foo(
<<<'CSS'>#someID{width: 100px; height: 100px}<CSS>>>,
<<<'JS'>alert("simple alert");<JS>>>,
<<<'HTML'><div
id="someID">$SomeOtherInnerHTML</div><HTML>>>
);
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66303&edit=1