|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-08-26 21:00 UTC] mageia at mokraemer dot de
Description:
------------
In previous versions it was possible to access the php compiler via create_function to make syntax checks on (generated) sources. Since this function is deprecated, there is no function to check php files for syntax errors in php.
Unsing this function is a hack, I know. But using include,... will cause php to call the shutdown function. What I want, is just parsing of the file, and receiving an error object which shows me if and where errors are.
Test script:
---------------
$code='<?php my php script with errors;?>';
$var = create_function('', '?>' . preg_replace(['|use [\w,\s\\\\]*;|'], '', $code) . '<?php ');
if(empty($var) && ( $error = error_get_last() )){
echo 'compile error. Details:';
print_r($error);
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 08:00:01 2025 UTC |
I'm going to step up and say wontfix on this. The main problem here is that PHP isn't designed to work in a way where it can syntactically validate code and not allow that code to impact the runtime as well. There are assorted performance enhancements and features that assume code being inspected is supposed to be (potentially) executed. For example, class and function definitions are normally hoisted and that happens before any code is executed. Refactoring PHP to support another interstitial layer where code is validated before anything gets interpreted seems to me like a lot of effort for little gain. Consider that you can already `php -l` a file, plus there are third-party libraries out there that are made to do this. While it's definitely not a good suggestion, note that create_function is basically just a wrapper around eval()ing a function definition so if you don't mind ignoring common sense then there's not much to stop you from doing eval('function() { ?>' . preg_replace(['|use [\w,\s\\\\]*;|'], '', $code) . '<?php };'); If you feel strongly about adding something that can be accessed from PHP code, check out the RFC process. https://wiki.php.net/rfc (though the mailing lists are down...)Test-Script: <?php function shutdown(){ echo 'shutdown'; } register_shutdown_function('shutdown'); $code = '?;'; try{ $var = eval('function() { ?>' . preg_replace(['|use [\w,\s\\\\]*;|'], '', $code) . ' <?php } ?>'); } catch (Exception $e){ echo 'exception catched'; print_r($e); } $php -v PHP 7.2.11 (cli) $php test.php shutdown => I get no exception to catch!because your code is simply wrong catch(Throwable $e) { }it works, period, refactor and simplify your code, you can catch parse errors as any other exception, starzing with PHP7 "Throwable"catches *anything* and if that don't happen you have other problems - this is a bugtracker and not a support forum --------------------------- [harry@srv-rhsoft:/downloads]$ php test.php exception catchedParseError Object ( [message:protected] => syntax error, unexpected end of file [string:Error:private] => [code:protected] => 0 [file:protected] => /mnt/data/downloads/test.php(4) : eval()'d code [line:protected] => 1 [trace:Error:private] => Array ( ) [previous:Error:private] => ) --------------------------- [harry@srv-rhsoft:/downloads]$ cat test.php <?php try { $var = eval('nonsense'); } catch (Throwable $e) { echo 'exception catched'; print_r($e); }