|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-06-26 10:46 UTC] d dot albano at gmail dot com
Description:
------------
Hi,
i've seen that set_error_handler doesn't let to the code to catch Parse Errors. I know that set_error_handler documentation page on php manual says that this error can't be catched, so this isin't a true bug: it is something like a logical bug because using strange tricks you can catch parse errors for included files.
So, instead to force php developers to do strange tricks to catch parse errors why don't try to send parse errors throught user defined error handler, if it is setted? I know that this isin't so simply but it is becoming necessary: in an advanced system is vitally catch any kind of error that can cause problem to page execution and this comprises catching every kind of errors that can be generated by third party module or every included file.
I understand that there isin't an easy way to do but a point of start can be define it through htaccess/php_value or simply use it only with included files after that set_error_handler is setted
Reproduce code:
---------------
# main.php:
<?php
function error_handler($errno, $errstr, $errfile, $errline)
{
echo 'error catched!';
}
set_error_handler('error_handler');
require_once('file_with_parse_error.php');
?>
# file_with_parse_error.php:
<?php
--- this is a voluntary php parse error ---
?>
Expected result:
----------------
Browser should show:
error catched!
Actual result:
--------------
Browser output:
Parse error: syntax error, ....
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 23 21:00:01 2025 UTC |
Looking to the code looks simply to do the modification, naturally, as you said, the problem is testing. However i founded an eval behaviour to support my feature request: if there is a parse error execution continues Can be that this is a bug or an unwanted behaviour, however nothing start to work strange after that a parse error was outputted. So, if all works with eval why it shouldn't work correctly with normal parse errors? Here there is some example code: <?php echo "BEGIN"; eval('echo "{$SYNTAX-ERROR"'); echo "END"; ?> It output BEGIN Parse error: syntax error, unexpected '-', expecting '}' in C:\web\htdocs\test.php(5) : eval()'d code on line 1 ENDThis classification is not bogus at all. Consider the following: == main.php == <?php $filename = sprintf("%s.php", 'myfile' ); include_once($filename); ?> == myfile.php == <?php syntax errors here !@#$%^&*()_ ?> The parsing of myfile.php must necessarily happen at main.php's RUNTIME, because the filename is not available until sprintf() is called. This is a bug.This can't be done safely within the same parser instance as your main script. You will need to create a separate instance, as in system("php -l $script"); to do that check. I would suggest you do this once when these scripts are created and move them into a "checked" directory or something so you don't do it on every include.Contrary to what others have said here, require/include_once do NOT (always) happen at compile time. Otherwise, it would be impossible to do this: <?php foreach(glob('/path/to/php/includes/*') as $match) { if (is_dir($match)) { foreach (glob($directory.'/*.php') as $filename) { require_once(($filename); } } else if (is_file($match)) { foreach (glob($directory.'/*.php') as $filename) { require_once(($filename); } } } ?> I've found the best way to work around this problem is to use eval, which is a poor solution at best. There really should be a way to catch this, considering PHP is an interpreted / templating language. Regardless of the intended functionality /doc of require/include_once there absolutely should be some mechanism to do this. See below for an example on how to at least silence parse errors: function safeInclude($filename) { $fc = file_get_contents($filename); eval('?>'.$fc); } However, in the defense of the developers, if you're checking code for errors before including it, you're one or more of the following: 1) Building convenience code to help with rapid development 2) Pulling in code that you don't fully trust from others that may be buggy 3) Doing silly, inexcusable things with production 4) Templating and thus #2 Fixing this bug likely be the most help to #1 and #4. If you're doing #4 you need to fire someone, if you're doing #3 you need to fire yourself. At the very least, if you're trying to do this you should review WHY you're trying to do this, and see if perhaps there's something else terribly wrong with your design approach. I'm a #4 and would like it if someone could look at fixing this.