This RFC proposes to add linemarkers to PHP at the compiler level.
Currently, anything which generates PHP from other input files of any kind must cope with the line numbers and file names in the resulting PHP file being potentially inaccurate.
Consider a scenario where a PHP script is only part of a larger file format, in this case PHPT.
--TEST-- Contrived example for an RFC. --FILE-- <?php This is a deliberate syntax error! ?> --EXPECT-- Succulent delights.
Now, run the test:
$ TEST_PHP_EXECUTABLE=`which php` php run-tests.php ./contrived-test.phpt ... $ cat ./contrived-test.log ... ---- ACTUAL OUTPUT Parse error: syntax error, unexpected 'is' (T_STRING) in contrived-test.php on line 2 ---- FAILED $
This RFC would allow run-tests.php, in its PHPT parser, to output something like:
# 4 "./contrived-test.phpt" <?php ...
And then the result of running the test:
$ TEST_PHP_EXECUTABLE=`which php` php run-tests.php ./contrived-test.phpt ... $ cat ./contrived-test.log ... ---- ACTUAL OUTPUT Parse error: syntax error, unexpected 'is' (T_STRING) in contrived-test.phpt on line 5 ---- FAILED $
The error message now locates the error much more usefully. The benefit is more obvious in larger files.
__FILE__
and __LINE__
macros__FILE__
cpp(1)
style.cat something.php | php -
The proposed syntax for linemarkers is identical to that used by the cpp(1)
command included with GCC:
# linenum “filename” flags
Alternatively, #line
could be used instead of a simple #
.
The flags field has the following potential values:
1 - Indicates the start of a new file.
2 - Indicates the return to a file after having switched to another.
This syntax has the advantage of being 100% backward-compatible with earlier versions of PHP, as the #
will be interpreted as starting a one-line comment.
Line markers which exist outside of <?php ?>
tags should be considered part of the enclosing HTML/XML and ignored as with all other such content.
No patch has been written yet, pending a determination of feasability and desirability.