且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-19 17:26:16

这是CodeIgniter 3.0.0中的一个已知问题,请参阅github问题此处 changelog 以下:


PHP 7下的新错误异常的错误处理(#4137) - :doc:错误处理< general / errors>


这是因为set_exception_handler()
$ b $ b set_exception_handler()使用Exception的类型声明,
会在抛出Error对象时导致致命错误。



如果处理程序需要同时工作PHP 5和7,你应该从处理程序中删除
类型声明,而正在执行
的代码只能通过Throwable替换为Exception
类型声明。

 <?php 
// PHP 5时代代码会破解。
function handler(Exception $ e){...}
set_exception_handler('handler');

// PHP 5和7兼容。
function handler($ e){...}

//仅限PHP 7。
function handler(Throwable $ e){...}
?>


升级到3.0.2以外的任何版本都能解决您的问题。 / p>

I was using CodeIgniter 3.0.0 with PHP 5.6.

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

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

This is a know issue in CodeIgniter 3.0.0, see the github issue here and changelog below:

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

It's because set_exception_handler() changed behavior in 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) { ... }
?>

Upgrading to anything beyond 3.0.2 will fix your issue.