I have a file called functions.php
.
This file consists includes to all the other function files, for example:
include_once("user_functions.php");
include_once("foo_functions.php");
I would like to catch errors where when I screw a code in one of those files, It wouldn't give the error to the entire system.
For example, if there is a parser error in foo_functions.php
it will just not include it in functions.php
.
Is that possible?
Parser errors are fatal errors, so you can't
catch
them. See this question and answer for more details.What you can do if you can run
exec()
is callphp -l thefilename.php
and check the result. See the manual for information on how this works. There are a few problems here, however:exec()
is often disabled, as it should be, because of the extremely high security risks of using it incorrectly.This code can check if file is exist or not, if file exist than include it.
And this one help you get syntax errors (PHP 7+ only)
so if you use PHP 7+ you may use:
include()
andinclude_once()
returnfalse
if they fail. You can use this to check if theinclude
d files were successful.By changing the
echo
s to handle your error logic, you should be able to do what you are asking.What if you put
at the beginning of foo_functions.php ?
The solution that I am using feels like a band-aid solution, but it will give you control back.
The idea is to use "eval()" to first check for errors. Also, ignore errors with the @ in the beginning. Of course you will need to be careful with eval, so don't ever let users feed anything to it.
Please note that I think "
file_get_contents
+eval
" = "require
", or very close to it, so you may be able to just skip the require-part.As of PHP 7, most eval/include errors, such as ParseError can be catched: