This is an old revision of the document!
PHP RFC: Loosening heredoc/nowdoc scanner
- Version: 0.9
- Date: 2014-08-29
- Author: Tjerk Meesters, datibbaw@php.net
- Status: Draft
- First Published at: http://wiki.php.net/rfc/heredoc-scanner-loosening
Introduction
Currently the rules for ending a heredoc or nowdoc are quite restrictive, requiring a newline after the closing identifier; this proposal aims to loosen or altogether remove that restriction.
Proposal
A fix for the following scenario:
eval("return <<<EOS\nfoo\nEOS;");
The above just illustrates the issue and doesn't advocate using eval().
Allow more liberal use in array constructs:
$strings = [<<<EOS a EOS, <<<EOS b EOS];
Or class constants:
class Test { const A = <<<EOS ab EOS . <<<EOS cd EOS; }
Loosened restrictions
Ends a quotation as soon as a newline is encountered as well as a few extra characters, i.e. a space, tab, period (concat), comma, ending square bracket (arrays) or null byte (end of file).
Removed restrictions
Ends a quotation as soon as the closing identifier is encountered.
Backward Incompatible Changes
Depending on whether we choose to loosen the restrictions (defined above) or completely remove it, the following behaviour will be changed:
Loosened restrictions
The following test code (taken from the aptly called “torture the T_END_HEREDOC rules) will not work as expected:
print <<<ENDOFHEREDOC ENDOFHEREDOC ; ENDOFHEREDOC; ENDOFHEREDOC ENDOFHEREDOC $ENDOFHEREDOC; ENDOFHEREDOC;
It emits “ENDOFHEREDOC ;\nENDOFHEREDOC;” and then stop scanning, leading to a parse error on the next line.
Removed restrictions
Removing the restrictions altogether will cause issues like this:
$s = <<<EOS Foo bar EOSBLA EOS;
It emits “Foo bar” and then stops scanning, leading to a parse error at “BLA”.
Proposed PHP Version(s)
PHP 7
Unaffected PHP Functionality
It doesn't impact the rules that govern the contents inside the heredoc or nowdoc.
Proposed Voting Choices
Should the heredoc and nowdoc scanner be changed?
Voting choices will be: - No, leave the scanner as it is. - Yes, loosen the newline restriction with a selected set of characters. - Yes, remove the newline restriction altogether.
This proposal requires a 2/3 majority as it affects the language.
Note: Both “Yes” options count towards changing the current behaviour; if a single majority for the last option can't be reached, the “loosened restrictions” will be applied.
Patches and Tests
The RFC author will provide the patches.