1

PHP 5 has error_get_last. Is there any way to completely or at least partially replicate the same functionality in PHP4.3?

Sam
  • 14,642
  • 6
  • 27
  • 39

2 Answers2

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