Note that there are some explanatory texts on larger screens.

plurals
  1. POerror_get_last() and custom error handler
    text
    copied!<p><a href="http://es.php.net/manual/en/function.odbc-errormsg.php" rel="nofollow"><code>odbc_errormsg</code></a> does not report error messages from <a href="http://php.net/odbc_execute" rel="nofollow"><code>odbc_execute</code></a> the way it's supposed to. It merely throws a warning. So I've been forced to write a hack to parse the error message via <a href="http://www.php.net/manual/en/function.error-get-last.php" rel="nofollow"><code>error_get_last</code></a>.</p> <p>I'm using <a href="http://www.php.net/manual/en/function.set-error-handler.php" rel="nofollow"><code>set_error_handler</code></a> and <code>error_get_last</code> returns <code>NULL</code> unless I either:</p> <ol> <li><p>disable my error handler,</p></li> <li><p>or make it return <code>FALSE</code>.</p></li> </ol> <p>I'd suppose this is due to PHP's builtin error handler taking care of storing the error details somewhere so they can be retrieved later.</p> <p><strong>Is there a way to emulate such behaviour in my custom error handler so <code>error_get_last()</code> can be used normally?</strong></p> <p><em>Please note I already know several ways to retrieve error info at any time. My question is how to make <code>error_get_last</code> usable.</em></p> <hr> <p><strong>Update:</strong> I think I'd better post some code.</p> <p>PHP has <code>error_get_last()</code>, which allows to do this:</p> <pre><code>@fopen('xxx'); var_dump( error_get_last() ); </code></pre> <p>... and get this:</p> <pre><code>array(4) { ["type"]=&gt; int(2) ["message"]=&gt; string(46) "fopen() expects at least 2 parameters, 1 given" ["file"]=&gt; string(69) "C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php" ["line"]=&gt; int(3) } </code></pre> <p>This breaks if you replace the builtin error handler:</p> <pre><code>function custom_error_handler($errno, $errstr, $errfile, $errline){ $ignore = ($errno &amp; error_reporting()) == 0; if(!$ignore){ echo "[Error happened: $errstr]\n"; } return TRUE; } set_error_handler('custom_error_handler'); @fopen('xxx'); var_dump( error_get_last() ); // NULL </code></pre> <p>If you keep both error handlers...</p> <pre><code>function custom_error_handler($errno, $errstr, $errfile, $errline){ $ignore = ($errno &amp; error_reporting()) == 0; if(!$ignore){ echo "[Error happened: $errstr]\n"; } return FALSE; } set_error_handler('custom_error_handler'); error_reporting(E_ALL); echo $foo; </code></pre> <p>... you get side effects:</p> <pre><code>[Error happened: Undefined variable: foo] Notice: Undefined variable: foo in C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php on line 15 Call Stack: 0.0004 329720 1. {main}() C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php:0 </code></pre> <p>... instead of just:</p> <pre><code>[Error happened: Undefined variable: foo] </code></pre> <p>I want to my custom error handler to interface properly with <code>error_get_last</code>. I want <code>error_get_last</code> to work fine.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload