错误处理和日志记录

debug_backtrace

<?php

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    var_dump(debug_backtrace());
}

foo();

debug_print_backtrace

<?php

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    debug_print_backtrace();
}

foo();

error_clear_last

<?php

error_clear_last();

error_get_last

<?php

var_dump(error_get_last());

error_log

<?php

error_log('Error!', 0);
error_log('Error!', 1, 'example@example.com');
error_log('Error!', 3, __DIR__ . '/error.log');

error_reporting

<?php

error_reporting(0);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ALL);
error_reporting(-1);
error_reporting(E_ALL ^ E_NOTICE);

restore_error_handler

<?php

/**
 * Test the error handling.
 *
 * @param  int    $errno
 * @param  string $errstr
 * @param  string $errfile
 * @param  int    $errline
 * @return void
 */
function foo(int $errno, string $errstr, string $errfile, int $errline)
{
    var_dump(__FUNCTION__);
}

/**
 * Test the error handling.
 *
 * @param  int    $errno
 * @param  string $errstr
 * @param  string $errfile
 * @param  int    $errline
 * @return void
 */
function bar(int $errno, string $errstr, string $errfile, int $errline)
{
    var_dump(__FUNCTION__);
}

set_error_handler('foo');
set_error_handler('bar');
trigger_error('Notice!');
// string(3) "bar"

var_dump(restore_error_handler()); // bool(true)
trigger_error('Notice!');
// string(3) "foo"

restore_exception_handler

<?php

/**
 * Test the exception handling.
 *
 * @param  Exception $exception
 * @return void
 */
function foo(Exception $exception)
{
    var_dump(__FUNCTION__);
}

/**
 * Test the exception handling.
 *
 * @param  Exception $exception
 * @return void
 */
function bar(Exception $exception)
{
    var_dump(__FUNCTION__);
}

set_exception_handler('foo');
set_exception_handler('bar');
var_dump(restore_exception_handler()); // bool(true)
throw new Exception('Uncaught Exception!');
// string(3) "foo"

set_error_handler

<?php

/**
 * Test the error handling.
 *
 * @param  int    $errno
 * @param  string $errstr
 * @param  string $errfile
 * @param  int    $errline
 * @return void
 */
function foo(int $errno, string $errstr, string $errfile, int $errline)
{
    var_dump(compact(['errno', 'errstr', 'errfile', 'errline']));
}

set_error_handler('foo');
trigger_error('Notice!');

set_error_handler('foo', E_USER_NOTICE);
trigger_error('Notice!', E_USER_NOTICE);

set_error_handler('foo', E_USER_ERROR);
trigger_error('Error!', E_USER_NOTICE);

set_exception_handler

<?php

/**
 * Test the exception handling.
 *
 * @param  Exception $exception
 * @return void
 */
function foo(Exception $exception)
{
    var_dump($exception->getMessage());
}

set_exception_handler('foo');
throw new Exception('Uncaught Exception!');
// string(19) "Uncaught Exception!"

trigger_error

<?php

trigger_error('Notice!');                  // PHP Notice:  Notice!
trigger_error('Notice!', E_USER_NOTICE);   // PHP Notice:  Notice!
trigger_error('Warning!', E_USER_WARNING); // PHP Warning:  Warning!
trigger_error('Error!', E_USER_ERROR);     // PHP Fatal error:  Error!

user_error

<?php

user_error('Notice!');                  // PHP Notice:  Notice!
user_error('Notice!', E_USER_NOTICE);   // PHP Notice:  Notice!
user_error('Warning!', E_USER_WARNING); // PHP Warning:  Warning!
user_error('Error!', E_USER_ERROR);     // PHP Fatal error:  Error!

results matching ""

    No results matching ""