PHP 5 has error_get_last. Is there any way to completely or at least partially replicate the same functionality in PHP4.3?
Asked
Active
Viewed 520 times
2 Answers
3
Ripped from the PHP manual (courtesy of php at joert dot net):
<?php
if( !function_exists('error_get_last') ) {
set_error_handler(
create_function(
'$errno,$errstr,$errfile,$errline,$errcontext',
'
global $__error_get_last_retval__;
$__error_get_last_retval__ = array(
\'type\' => $errno,
\'message\' => $errstr,
\'file\' => $errfile,
\'line\' => $errline
);
return false;
'
)
);
function error_get_last() {
global $__error_get_last_retval__;
if( !isset($__error_get_last_retval__) ) {
return null;
}
return $__error_get_last_retval__;
}
}
?>

Chris Eberle
- 47,994
- 12
- 82
- 119
-
Also interesting to learn about create_function from the answer. Looks kind of nasty. – Sam Jul 03 '11 at 21:03
0
Yes it is, but you will have to do some programming, you need to attach error handler
$er_handler = set_error_handler("myErrorHandler");
but before this you need to write your "myErrorHandler"
function myErrorHandler($errNumber, $errString, $errFile, $errLine)
{
/*now add it to session so you can access it from anywhere, or if you have class with the static variable you can save it there */
$_SESSION["Error.LastError"] = $errNumber . '<br>' . $errString . '<br>' . $errFile . '<br>' . $errLine;
}
Now when error is occured you can get it by
if(isset($_SESSION["Error.LastError"]))
$str = $_SESSION["Error.LastError"];
now to replicate your method you need to create function
function get_last_error()
{
$str = "";
if(isset($_SESSION["Error.LastError"]))
$str = $_SESSION["Error.LastError"];
return $str;
}

Senad Meškin
- 13,597
- 4
- 37
- 55
-
+1, I like the idea, but I don't think `error_get_last` persists between sessions (it's just within the context of the currently executing environment). Probably better just to store it in a global variable or something. – Chris Eberle Jul 01 '11 at 14:18
-
error_get_last doesn't exist in php 4.3.xx but this is the way to keep your last error while session exists. – Senad Meškin Jul 01 '11 at 14:19
-
I'm saying that the REAL `error_get_last` only applies to the current script. It doesn't last from one connection to the next. – Chris Eberle Jul 01 '11 at 14:21
-
jap, u are correct, but you can always set after session_start(); unset of Error.LastError so it will be valid only for same request. – Senad Meškin Jul 01 '11 at 14:23
-
-
-
not when they're appropriate. Besides, what on earth do you think `$_SESSION` is? Practice what you preach there, buddy. – Chris Eberle Jul 01 '11 at 14:31