How to capture the error on the function "enable" PHP?

I need to fix the last error in the function "enable" PHP.

I am testing the exceptions function, but unfortunately I wrote over the include function.

If I write after the function "include" does not display an exception.

Example 1:

try{ throw new exception(); require_once( $this->controller['path'] ); } catch( exception $e ) { print_r( error_get_last() ); } 

This is the return: ... (Void) ...

Example 2:

 try{ require_once( $this->controller['path'] ) OR throw new exception();; } catch( exception $e ) { print_r( error_get_last() ); } 

This is a return: Parse error: syntax error, unexpected T_THROW in ...

I intentionally created a syntax error in the file to include. The idea is to catch a bug so that you can debug it.

Does anyone know how to get this?

Guys! I need to catch syntax errors. Hello!

+4
source share
4 answers

Firstly, if you use require , it will always kill your application if the file cannot be included. There is no control over the result, you can do nothing after that. If you want to monitor the success of file inclusions, use include and check its return value.

 $success = include "foo.php"; if (!$success) { // the file could not be included, oh noes! } 

You may have syntax differences, for example:

 if (!(include "foo.php")) ... 

This will trigger E_NOTICE if the file cannot be included, which you cannot catch. But this is normal, as it will help you debug the problem, and the error display will be disabled in the process anyway (right, right?).

or throw new Exception does not work because throw is an operator and cannot be used where an expression is expected.


If you want to catch syntax errors in the included file, use php_check_syntax / it successor php -l <file> .

+6
source

require_once is a language construct, not a function, and you cannot short circuit it.

To check if a file exists before including it, you can use is_file() . If you want to check if a file is being read, you can use is_readable() .

You can also use include , which E_WARNING if the file is not found, and not E_COMPILE_ERROR .

+4
source

i agree with alex, try cath error with checking file_exists() , then use Exception

 if(file_exists($this->controller['path'])){ try{ require_once( $this->controller['path'] ); }catch(Exception $e){ // throw error } } 

or use is_readable ()

 if(is_readable($this->controller['path'])){ try{ require_once( $this->controller['path'] ); }catch(Exception $e){ // throw error } } 
+1
source

Most PHP built-in functions throw errors, and do not throw exceptions. To convert PHP errors to exceptions, you can configure your own error handler:

 function exceptions_error_handler($severity, $message, $filename, $lineno) { throw new ErrorException($message, 0, $severity, $filename, $lineno); } set_error_handler('exceptions_error_handler'); 

With this code, you can catch all non-fatal errors as exceptions. Note that the require_* functions cause a fatal error, so you have to use include .

As for handling syntax errors inside the code itself, it is completely impossible if you think about it a bit.

0
source

Source: https://habr.com/ru/post/1387501/


All Articles