HEREDOC in psr

555 views
Skip to first unread message

Chuck Reeves

unread,
Feb 24, 2013, 8:51:07 AM2/24/13
to php...@googlegroups.com
There is no spec about how this should be handled.  Is it discouraged?  Can we embed them in functions or put before or after class definitions?  Any thoughts about this?

This came to mind while im working on a ZF2 project using mongo and some map reduce functions.  I want to use heredoc to define the functions for readability.

Larry Garfield

unread,
Feb 24, 2013, 1:23:53 PM2/24/13
to php...@googlegroups.com
I rarely use HEREDOC, but I don't think it's necessarily evil to use if
the situation calls for it. The language syntax itself is actually
quite rigid about how you can use it, so I don't think there's room for
further standardization beyond what the language imposes.

--Larry Garfield
> --
> You received this message because you are subscribed to the Google
> Groups "PHP Framework Interoperability Group" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to php-fig+u...@googlegroups.com.
> To post to this group, send email to php...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/php-fig/-/CqEaPYVLpuYJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

FGM at GMail

unread,
Feb 24, 2013, 1:40:31 PM2/24/13
to php...@googlegroups.com
If we do want to add to the language, there's also NOWDOC syntax to
take into account.

I tend to use heredocs mostly for SQL, BTW ; something like:
<?php
$request = <<<SQL
(some sql)
SQL;

makes a very readable delimitation of the SQL from the enclosing PHP code.

2013/2/24 Larry Garfield <la...@garfieldtech.com>:

Sebastian Krebs

unread,
Feb 24, 2013, 2:17:11 PM2/24/13
to php...@googlegroups.com



2013/2/24 FGM at GMail <fgma...@gmail.com>

If we do want to add to the language, there's also NOWDOC syntax to
take into account.

I tend to use heredocs mostly for SQL, BTW ; something like:
<?php
$request = <<<SQL
(some sql)
SQL;

makes a very readable delimitation of the SQL from the enclosing PHP code.

Also some ideas (namely PhpStorm (don't now, if any other is capable of this)) is able to inject code highlighting for heredoc-strings with special heredoc-markers like "<<<SQL" for SQL and "<<<JS" for Javascript. 

justin

unread,
Feb 24, 2013, 3:33:38 PM2/24/13
to php...@googlegroups.com
On Sun, Feb 24, 2013 at 11:17 AM, Sebastian Krebs <kreb...@gmail.com> wrote:

2013/2/24 FGM at GMail <fgma...@gmail.com>
If we do want to add to the language, there's also NOWDOC syntax to
take into account.

I tend to use heredocs mostly for SQL, BTW ; something like:
<?php
$request = <<<SQL
(some sql)
SQL;

makes a very readable delimitation of the SQL from the enclosing PHP code.

Also some ideas (namely PhpStorm (don't now, if any other is capable of this)) is able to inject code highlighting for heredoc-strings with special heredoc-markers like "<<<SQL" for SQL and "<<<JS" for Javascript. 


Sublime Text does it automatically if your heredoc starts with 'SELECT', 'INSERT', etc.

--justin 

Paul Dragoonis

unread,
Feb 24, 2013, 6:28:51 PM2/24/13
to php...@googlegroups.com
These seem like cool IDE enhancements, but as Larry has already pointed out, the language syntax on using heredoc's is already quite strict and there's nothing really to define here, call your heredoc what you like and whatever makes sense to you in your application.

We're here to standardize commonalities between our projects and since heredoc's aren't really used by many of these projects then it isn't really a priority to work on.


--

Jason Judge

unread,
Feb 26, 2013, 8:07:11 PM2/26/13
to php...@googlegroups.com
Personally I'm finding heredocs useful on some WordPress projects (which generally encourage HTML, JavaScript etc embedded in PHP), but the main problem is the indentation. As soon as you enter a heredoc, you are entering an embedded document with its own indentation and indenting rules, and that can be quite difficulty to follow at times. So my heredocs end up being moved out to external files (templates, I guess).

    $myhtml = include('mytemplate.php');

    mytemplate.php:
    <?php
    // Initialise/escape template variables
    return <<<HTML
    <div>whatever</div>
    HTML;

I'm not sure if that example helps, but in general I feel it means heredocs should in general be discouraged in mainly PHP scripts as it can make the code more difficult to follow, but you cannot deny it helps testing some quick hacks. For the amount of use it gets, I don't think it would help to be mentioned in a PSR.

-- Jason

Sebastian Krebs

unread,
Feb 27, 2013, 3:55:23 AM2/27/13
to php...@googlegroups.com



2013/2/27 Jason Judge <jason...@consil.co.uk>

Personally I'm finding heredocs useful on some WordPress projects (which generally encourage HTML, JavaScript etc embedded in PHP), but the main problem is the indentation. As soon as you enter a heredoc, you are entering an embedded document with its own indentation and indenting rules, and that can be quite difficulty to follow at times. So my heredocs end up being moved out to external files (templates, I guess).

    $myhtml = include('mytemplate.php');

    mytemplate.php:
    <?php
    // Initialise/escape template variables
    return <<<HTML
    <div>whatever</div>
    HTML;

I'm not sure if that example helps, but in general I feel it means heredocs should in general be discouraged in mainly PHP scripts as it can make the code more difficult to follow, but you cannot deny it helps testing some quick hacks. For the amount of use it gets, I don't think it would help to be mentioned in a PSR.

Don't blame the messenger :)
Of course you can mess up your scripts with heredoc/nowdoc, but you can mess up your scripts with _everything_. There are impressive examples for overlong variables names, or hyper-nested ternary-operator-chains, but for all of them: They are only bad, when you use them wrong. But to stick with strings

$html = '<html><head><title>' . $title . '</title>';
foreach ($metas as $meta) $html .= $meta;
// snip
$html .= '</head><body>';
$html .= Templates::render('header');
// super-snip
$html .= Templates::render('footer');
echo $html;
return $html;

Yes, I've seen ... things. This is an example (written from memory), that was the result of discouraging heredoc, double-quote-strings and a template-engine (because the only known to "the team" was smarty and nobody wanted to use it at the end :D)

(Btw: I still don't know, why both "echo" and "return" was used...)


I would discourage to discourage heredoc ;)
 

-- Jason


On Sunday, 24 February 2013 13:51:07 UTC, Chuck Reeves wrote:
There is no spec about how this should be handled.  Is it discouraged?  Can we embed them in functions or put before or after class definitions?  Any thoughts about this?

This came to mind while im working on a ZF2 project using mongo and some map reduce functions.  I want to use heredoc to define the functions for readability.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/php-fig/-/JE2EVEpf_4gJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages