before
Fatal error: Uncaught Error: Class 'a' not found in D:\laragon\www\php_book\test.php on line 12
Error: Class 'a' not found in D:\laragon\www\php_book\test.php on line 12
Call Stack:
0.0020 360760 1. {main}() D:\laragon\www\php_book\test.php:0
这个是中止方法test的输出
<?php
class Shutdown
{
public function stop()
{
echo "这个是stop方法的输出";
}
}
// 当PHP终止的时候(执行完成或者是遇到致命错误中止的时候)会调用new Shutdown的stop方法
register_shutdown_function([new Shutdown(), 'stop']);
// 将因为致命错误而中止
$a = new a();
// 这一句并没有执行,也没有输出
echo '必须终止';
也可以在类中执行:
<?php
class TestDemo {
public function __construct()
{
register_shutdown_function([$this, "f"], "hello");
}
public function f($str)
{
echo "class TestDemo->f():" . $str;
}
}
$demo = new TestDemo();
echo 'before' . PHP_EOL;
/**
运行:
before
class TestDemo->f():hello
*/
<?php
/**
* register_shutdown_function,注册一个会在php中止时执行的函数,中止的情况包括发生致命错误、die之后、exit之后、执行完成之后都会调用register_shutdown_function里面的函数
* Created by PhpStorm.
* User: Administrator
* Date: 2017/7/15
* Time: 17:41
*/
class Shutdown
{
public function stop()
{
echo 'Begin.' . PHP_EOL;
// 如果有发生错误(所有的错误,包括致命和非致命)的话,获取最后发生的错误
if (error_get_last()) {
print_r(error_get_last());
}
// ToDo:发生致命错误后恢复流程处理
// 中止后面的所有处理
die('Stop.');
}
}
// 当PHP终止的时候(执行完成或者是遇到致命错误中止的时候)会调用new Shutdown的stop方法
register_shutdown_function([new Shutdown(), 'stop']);
// 将因为致命错误而中止
$a = new a();
// 这一句并没有执行,也没有输出
echo '必须终止';
运行:
Fatal error: Uncaught Error: Class 'a' not found in D:\laragon\www\php_book\1_23_register_shutdown.php on line 31
Error: Class 'a' not found in D:\laragon\www\php_book\1_23_register_shutdown.php on line 31
Call Stack:
0.0060 362712 1. {main}() D:\laragon\www\php_book\1_23_register_shutdown.php:0
Begin.
Array
(
[type] => 1
[message] => Uncaught Error: Class 'a' not found in D:\laragon\www\php_book\1_23_register_shutdown.php:31
Stack trace:
#0 {main}
thrown
[file] => D:\laragon\www\php_book\1_23_register_shutdown.php
[line] => 31
)
Stop.
Error Object
(
[message:protected] => Class 'a' not found
[string:Error:private] =>
[code:protected] => 0
[file:protected] => C:\laragon\www\php_book\throwable.php
[line:protected] => 5
[trace:Error:private] => Array
(
)
[previous:Error:private] =>
[xdebug_message] =>
Error: Class 'a' not found in C:\laragon\www\php_book\throwable.php on line 5
Call Stack:
0.0000 349856 1. {main}() C:\laragon\www\php_book\throwable.php:0
)
Class 'a' not found