ErrorException returns invalid file and string

in file1.php file:

set_error_handler('my_error_handler'); set_exception_handler('my_exception_handler'); function my_error_handler($errNo, $errStr, $errFile, $errLine, $whatever = null){ // here ErrFile and $errLine are correct only for native funcs... throw New ErrorException($errStr, 0, $errNo, $errFile, $errLine); } function my_exception_handler($exception){ print $exception->getLine(); // correct only for native functions print $exception->getFile(); // correct only for native functions } 

in file2.php, an example of a function definition:

 function foo($requiredArg){ } 

and in the file 3.php, calling foo:

  foo(); 

gives:

The argument 1 for foo () is missing, called in the file 3.php on line 2 and is defined ...

The message is useless, but when I try to get the file and line with $exception->getFile() and $exception->getLine() (in my exception handler), I get the file and the line where foo () was defined, and not there where he was called ...

But with native PHP functions, I get the file and the line where the function was called (and this is what I want).

+4
source share
2 answers

Good question!

Things seem to behave differently for their own custom functions. One obvious thing, though, cannot really tell you where a function is defined, if it is native, because it is written in C and not in PHP. It would be nice to know where this was called in the case of user space code. One way is to simply parse this error message from the caught exception, you can get the file and the line in which it was created.

 function my_exception_handler($exception) { $message = $exception->getMessage(); $file = $exception->getFile(); $line = $exception->getLine(); if(strpos($message, 'called in') !== false) { preg_match('/called in .*php/', $message, $matches); $file = str_replace('called in ', '', $matches[0]); preg_match('/on line \d/', $message, $matches); $line = str_replace('on line ', '', $matches[0]); } echo 'line: ' . $line . PHP_EOL; echo 'file: ' . $file . PHP_EOL; } 

Image based on the result with my_exception_handler changed:

 line: 4 file: /tmp/file3.php 
+1
source

It just comes down to the closest.

When you use a kernel or built-in function, their definitions are included in the PHP core. This would mean that the function definition is not available compared to a user-defined function, the definition of which is easily accessible.

If you run debug_backtrace(); or $exception->getTrace(); inside your exception_handler function, which refers to your custom function error, you will see the function definition first and the second function call. If you call an internal function, you will only see the calling line, since PHP cannot specify a line of code in your script or the included files defined by the internal function because it is defined in the kernel.

This is the error handling routine. If you want to get the actual row that called the function, you can view the trace and grab the link to the second array to the row number.

+2
source

All Articles