您的位置 首页 技术专栏

聊聊在PHP7中对于Error的处理是怎样的

24小时课堂在线收录聊聊在PHP7中对于Error的处理是怎样的,前段时间在项目中遇到一个当时觉得比较奇怪的情况:使用 GuzzleHttp 发送 curl 请求,API 响应超时导致抛出异常。但 catc…感谢您关注聊聊在PHP7中对于Error的处理是怎样的。

前段时间在项目中遇到一个当时觉得比较奇怪的情况:使用 GuzzleHttp 发送 curl 请求,API 响应超时导致抛出异常。但 catch(\Exception) 并没有捕获异常,导致代码意外停止运行。后来查资料发现,在 PHP 7 中,GuzzleHttp 请求超时抛出的异常继承的是 Error,而 Error 并没有继承 Exception,所以 catch(\Exception) 无法捕获并处理该异常。

PHP 7 中对 Error 的处理

在 PHP 5 中,当程序中有致命错误发生时,脚本会立即停止运行。并且,通过 set_error_handler 设置的错误处理程序在这种情况下并不会被调用。

⒈ 自定义错误处理程序 set_error_handler

set_error_handler 接受两个参数,第一个为自定义的错误处理函数,第二个参数指定触发该自定义错误处理函数的错误级别。但需要指出的是,在任何时候,只能有一个自定义的错误处理程序起作用。

function func_notice($num, $str, $file, $pne) { print “Encountered notice $num in $file, pne $pne: $str\n”;}function func_error($num, $str, $file, $pne) { print “Encountered error $num in $file, pne $pne: $str\n”;}set_error_handler(“func_notice”, E_NOTICE);set_error_handler(“func_error”, E_ERROR);echo $foo;

以上代码在执行以后,会输出 PHP Notice: Undefined variable: foo 。在第二个 set_error_handler 执行以后,自定义错误处理函数变成了 func_error ,同时,触发自定义错误处理函数的错误级别变成了 E_ERROR 。而在 PHP 中,变量未定义只会触发 E_NOTICE 级别的错误,所以自定义的错误处理函数并不会被触发。

需要指出的是,自定义的错误处理函数对以下几种错误级别并不起作用:

E_ERROR、E_PARSE、E_CORE_ERROR、E_CORE_WARNING、E_COMPILE_ERROR、E_COMPILE_WARNING、E_STRICT

在上述几种自定义错误处理程序无法处理的错误中,凡是以 ERROR 结尾的都是致命错误。其他几种虽然不是致命错误,但

E_PARSE 是在解析 PHP 代码时产生的错误,此时 PHP 代码尚未开始运行,自定义错误处理程序自然无法处理该错误

E_CORE_WARNING 产生于 PHP 的初始化启动阶段,此时 PHP 代码仍然尚未运行,所以不能被自定义错误处理程序处理

E_COMPILE_WARNING 是在 PHP 代码的编译阶段产生,所以不能被自定义错误处理程序处理

而至于 E_STRICT 是 PHP 为了保证代码的最佳互操作性和向前兼容而提出的代码修改建议,自然也不会被自定义错误处理函数处理

function func_error($num, $str, $file, $pne) { print “Encountered error $num in $file, pne $pne: $str\n”;}set_error_handler(‘func_error’, E_NOTICE);$obj = ‘foo’;$obj->method();

以上代码运行输出结果:

PHP Fatal error: Call to a member function method() on string

虽然设置了自定义错误处理程序,但在致命错误发生时,并不起作用。

对于这种自定义错误处理程序无法处理的致命错误,在 PHP 5 中可以通过注册一个终止回调(shutdown_function)来记录具体的错误信息,但也仅限于记录错误信息,当发生致命错误时代码仍然会停止运行。

$shutdownHandler = function(){ print PHP_EOL; print “============================” . PHP_EOL; print “Running the shutdown handler” . PHP_EOL; $error = error_get_last(); if (!empty($error)) { print “Looks pke there was an error: ” . print_r($error, true) . PHP_EOL; // 可以添加记录日志的逻辑 } else { // 程序正常运行结束 print “Running a normal shutdown without error.” . PHP_EOL; }};register_shutdown_function($shutdownHandler);$obj = ‘foo’;$obj->method();

以上代码执行会输出

PHP Fatal error: Call to a member function method() on string in /home/chenyan/test.php on pne 24============================Running the shutdown handlerLooks pke there was an error: Array( [type] => 1 [message] => Call to a member function method() on string [file] => /home/chenyan/test.php [pne] => 24)

⒉ 撤销自定义错误处理程序

当同时设置多个自定义错误处理程序时,虽然只有最后设置的自定义错误处理程序起作用。但所有设置的自定义错误处理程序会以栈的方式保存(FILO)。

使用 restore_error_handler 可以撤销最近一次设置的自定义错误处理程序;如果同时调用了多次 set_error_handler ,则每调用一次 restore_error_handler,处于栈顶的错误处理程序就会被撤销。

function func_notice($num, $str, $file, $pne) { print “Encountered notice : $str\n”;}set_error_handler(“func_notice”, E_NOTICE);set_error_handler(“func_notice”, E_NOTICE);set_error_handler(“func_notice”, E_NOTICE);echo $foo;set_error_handler(“func_notice”, E_NOTICE);echo $foo;restore_error_handler();echo $foo;restore_error_handler();echo $foo;restore_error_handler();echo $foo;restore_error_handler();echo $foo;

以上代码运行,会输出:

Encountered notice : Undefined variable: fooEncountered notice : Undefined variable: fooEncountered notice : Undefined variable: fooEncountered notice : Undefined variable: fooEncountered notice : Undefined variable: fooPHP Notice: Undefined variable: foo

⒊ PHP 7 中对错误的处理

在 PHP 7 中,当有致命错误或 E_RECOVERABLE_ERROR 类型的错误发生时,通常会抛出一个 Error,程序并不会终止。

try { $obj = ‘foo’; $obj->method();} catch (\Error $e) { echo $e->getMessage();}

运行以上代码会输出

Call to a member function method() on string

E_RECOVERABLE_ERROR 是一种可捕获的致命错误,这种错误的出现并不会使得 Zend 引擎处于不稳定的状态,但必须被捕获并且处理。如果不处理,那么这种错误最终会变成 E_ERROR 类型的错误,最终导致 PHP 代码停止运行。

php 7 中,并不是所有的致命错误都会抛出 Error,一些特定情况下出现的致命错误( Out Of Memory)仍然会导致代码停止运行。另外,如果抛出的 Error 没有被捕获并处理,则代码仍然会停止运行。

// bak.sql 的大小为 377 M// PHP 配置的 memory_pmit = 128Mtry { $file = ‘./bak.sql’; file_get_contents($file);} catch (\Error $e) { echo $e->getMessage();}// 执行以上代码,仍然会产生致命错误PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 395191240 bytes)// 抛出的 Error 没有被捕获并处理,代码依然会停止运行$obj = ‘foo’;$obj->method();// 执行以上代码,由于并没有用 try/catch 捕获并处理抛出的 Error,程序仍然会停止运行PHP Fatal error: Uncaught Error: Call to a member function method() on string

PHP 7 中的 Error 并没有继承 Exception,之所以这样做是为了防止 PHP 5 中捕获并处理 Exception 的代码捕获这些 Error。因为在 PHP 5 中,这些致命错误是会导致代码停止运行的。

Error 和 Exception 都继承自 Throwable 。在 PHP 7 中,Throwable 是一个 interface,所有能通过 throw 关键字抛出的对象都实现了这个 interface。

interface Throwable{ pubpc function getMessage(): string; pubpc function getCode(): int; pubpc function getFile(): string; pubpc function getLine(): int; pubpc function getTrace(): array; pubpc function getTraceAsString(): string; pubpc function getPrevious(): Throwable; pubpc function __toString(): string;}

需要指出的是,Throwable 是 PHP 底层的 interface,PHP 代码中不能直接实现 Throwable 。之所以作出这个限制,是因为通常只有 Error 和 Exception 可以被抛出,并且这些抛出的 Error 和 Exception 中还存储了它们被抛出的堆栈跟踪信息,而 PHP 代码中开发者自定义的 class 无法实现这些。

要在 PHP 代码中实现 Throwable 必须通过继承 Exception 来实现。

interface CustomThrowable extends Throwable {}class CustomException extends Exception implements CustomThrowable {}throw new CustomException();

PHP 7 中 Error 和 Exception 的继承关系

interface Throwable |- Exception implements Throwable |- Other Exception classes |- Error implements Throwable |- TypeError extends Error |- ParseError extends Error |- AssertionError extends Error |- ArithmeticError extends Error |- DivizionByZeroError extends ArithmeticError

TypeError

当函数的传参或返回值的数据类型与申明的数据类型不一致时,会抛出 TypeError

function add(int $left, int $right){ return $left + $right;}try { $value = add(‘left’, ‘right’);} catch (TypeError $e) { echo $e->getMessage();}// 运行以上代码,会输出:Argument 1 passed to add() must be of the type int, string given

当开启严格模式时,如果 PHP 内建函数的传参个数与要求的参数不一致,也会抛出 TypeError

declare(strict_types = 1);try { substr(‘abc’);} catch (TypeError $e) { echo $e->getMessage();}// 运行以上代码,会输出:substr() expects at least 2 parameters, 1 given

默认情况下,PHP 7 处于弱模式。在弱模式下,PHP 7 会尽可能的将传参的数据类型转换为期望的数据类型。例如,如果函数期望的参数类型为 string,而实际传参的数据类型的 int,那么 PHP 会把 int 转换为 string。

// declare(strict_types = 1);function add(string $left, string $right){ return $left + $right;}try { $value = add(11, 22); echo $value;} catch (TypeError $e) { echo $e->getMessage();}// 以上代码运行,会正常输出 33,PHP 会对传参的数据类型做转换(int→string→int)// 但如将 PHP 改为严格模式,则运行是会抛出 TypeErrorArgument 1 passed to add() must be of the type string, int given

ParseError

当在 include 或 require 包含的文件中存在语法错误,或 eval() 函数中的代码中存在语法错误时,会抛出 ParseError

// a.php$a = 1$b = 2// test.phptry { require ‘a.php’;} catch (ParseError $e) { echo $e->getMessage();}// 以上代码运行会输出:syntax error, unexpected ‘$b’ (T_VARIABLE)// eval 函数中的代码存在语法错误try { eval(“$a = 1”);} catch (ParseError $e) { echo $e->getMessage();}// 以上代码运行会输出:syntax error, unexpected end of file

AssertionError

当断言失败时,会抛出 AssertionError(此时要求 PHP 配置中 zend.assertions = 1,assert.exception = 1,这两个配置可以在 php.ini 文件中配置,也可以通过 ini_set() 在 PHP 代码中配置)。

ini_set(‘zend_assertions’, 1);ini_set(‘assert.exception’, 1);try { $test = 1; assert($test === 0);} catch (AssertionError $e) { echo $e->getMessage();}// 运行以上代码会输出:assert($test === 0)

ArithmeticError

在 PHP 7 中,目前有两种情况会抛出 ArithmeticError:按位移动操作,第二个参数为负数;使用 intdiv() 函数计算 PHP_INT_MIN 和 -1 的商(如果使用 / 计算 PHP_INT_MIN 和 -1 的商,结果会自动转换为 float 类型)。

try { $value = 1 << -1;} catch (ArithmeticError $e) { echo $e->getMessage();}// 运行以上代码,会输出:Bit shift by negative numbertry { $value = intdiv(PHP_INT_MIN, -1);} catch (ArithmeticError $e) { echo $e->getMessage();}// 运行以上代码,会输出:Division of PHP_INT_MIN by -1 is not an integer

DivisionByZeroError

抛出 DivisionByZeorError 的情况目前也有两种:在进行取模(%)运算时,第二个操作数为 0;使用 intdiv() 计算两个数的商时,除数为 0。如果使用 / 计算两个数的商时除数为 0,PHP 只会产生一个 Warning。并且,如果被除数非 0,则结果为 INF,如果被除数也是 0,则结果为 NaN。

try { $value = 1 % 0; echo $value;} catch (DivisionByZeroError $e) { echo $e->getMessage(), “\n”;}// 运行以上代码,会输出:Modulo by zerotry { $value = intdiv(0, 0); echo $value;} catch (DivisionByZeroError $e) { echo $e->getMessage(), “\n”;}// 运行以上代码,会输出:Division by zero

通常在实际的业务中,捕获并处理抛出的 Error 并不常见,因为一旦抛出 Error 说明代码存在严重的 BUG,需要修复。所以,在实际的业务中,Error 更多的只是被用来捕获并记录具体的错误日志,然后通知开发者进行 BUG 修复。

以上就是聊聊在PHP7中对于Error的处理是怎样的的详细内容,更多请关注24小时课堂在线其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/97400.html

为您推荐

返回顶部