Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching PHP Parser error when using include

Tags:

php

try-catch

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?

like image 219
Jacob Cohen Avatar asked Jan 29 '14 12:01

Jacob Cohen


1 Answers

As of PHP 7, most eval/include errors, such as ParseError can be catched:

try {
  include_once(__DIR__ . '/test.php');
} catch (\Throwable $e) {
  var_dump($e);
}
like image 192
Alexander Shostak Avatar answered Sep 21 '22 19:09

Alexander Shostak