更新到 PHP 7 后 CodeIgniter CI_Exceptions::show_exception 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36982769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:00:52  来源:igfitidea点击:

CodeIgniter CI_Exceptions::show_exception error after updating to PHP 7

phpcodeignitercodeigniter-3php-7

提问by Jatin Dhoot

I was using CodeIgniter 3.0.0 with PHP 5.6.

我在 PHP 5.6 中使用 CodeIgniter 3.0.0。

Yesterday I updated to PHP 7 and started getting following error:-

昨天我更新到 PHP 7 并开始出现以下错误:-

Uncaught TypeError: Argument 1 passed to CI_Exceptions::show_exception() must be
 an instance of Exception, instance of Error given, called in /my/file/path/app/system/core/Common.php on line 658 and defined in /my/file/path/hgx_portal/app/system/core/Exceptions.php:190
Stack trace:
#0 /my/file/path/hgx_portal/app/system/core/Common.php(658): CI_Exceptions->show_exception(Object
(Error))
#1 [internal function]: _exception_handler(Object(Error))
#2 {main}
  thrown in /my/file/path/hgx_portal/app/system/core/Exceptions.phpon line 190

回答by Garry Welding

This is a know issue in CodeIgniter 3.0.0, see the github issue hereand changelogbelow:

这是 CodeIgniter 3.0.0 中的一个已知问题,请参阅此处的 github 问题和下面的更改日志

Fixed a bug (#4137) - :doc:Error Handling <general/errors>breaks for the new Error exceptions under PHP 7.

修复了一个错误 (#4137) - :doc:Error Handling <general/errors>打破 PHP 7 下的新错误异常。

It's because set_exception_handler() changed behaviorin PHP 7.

这是因为 set_exception_handler()在 PHP 7 中改变了行为

Code that implements an exception handler registered with set_exception_handler() using a type declaration of Exception will cause a fatal error when an Error object is thrown.

If the handler needs to work on both PHP 5 and 7, you should remove the type declaration from the handler, while code that is being migrated to work on PHP 7 exclusively can simply replace the Exception type declaration with Throwable instead.

<?php
// PHP 5 era code that will break.
function handler(Exception $e) { ... }
set_exception_handler('handler');

// PHP 5 and 7 compatible.
function handler($e) { ... }

// PHP 7 only.
function handler(Throwable $e) { ... }
?>

使用 Exception 类型声明实现使用 set_exception_handler() 注册的异常处理程序的代码将在抛出 Error 对象时导致致命错误。

如果处理程序需要在 PHP 5 和 7 上运行,您应该从处理程序中删除类型声明,而正在迁移到专门用于 PHP 7 的代码可以简单地用 Throwable 替换 Exception 类型声明。

<?php
// PHP 5 era code that will break.
function handler(Exception $e) { ... }
set_exception_handler('handler');

// PHP 5 and 7 compatible.
function handler($e) { ... }

// PHP 7 only.
function handler(Throwable $e) { ... }
?>

Upgrading to anything beyond 3.0.2 will fix your issue.

升级到 3.0.2 以外的任何内容都可以解决您的问题。

回答by Silvio Delgado

This error is caused by PHP 7 (which throws Errorinstead Exceptionin set_exception_handlerfunction.

此错误是由PHP 7(其抛出引起Error代替Exceptionset_exception_handler功能。

If you cannot do an upgrade the CodeIgniter system folder, you can just change the file system/core/Exceptions.phpat line 190:

如果您无法升级 CodeIgniter 系统文件夹,您可以更改文件system/core/Exceptions.php190

public function show_exception(Exception $exception)

To

public function show_exception($exception)