register_shutdown_function和set_error_handler函数调用在codeIgniter中的实现

文章讲述了在PHP中遇到`register_shutdown_function()`使用错误的问题,原因是传递给该函数的回调不是可调用的。解决方案是使用对象方法调用的语法,即`[$this,shutDownFunction]`。文章还提供了在CodeIgniter框架中设置全局错误和关闭处理函数的方法。
摘要由CSDN通过智能技术生成

警告:register_shutdown_function():无效的关闭回调

trait ErrorTrait {
        public function shutDownFunction() { 
            $error = error_get_last();
                // Fatal error, E_ERROR === 1
                if ($error['type'] === E_ERROR) { 
                    //do your stuff     
                    $messageStore="Using $this when not in object context";
                    if (strstr ( $error['message'],$messageStore))
                    {
                        echo "found it";
                    }
                } 
                }
        public function shutdown_function()
        {
        register_shutdown_function('shutDownFunction');
        } 

我在我的主类中使用这个特性并从中调用函数

  use ErrorTrait;
     public function test()
{   self::shutDownFunction();
    self::shutdown_function(); }

然后在这一点上,我在一个名为“run”的函数中调用test中的函数

我所做的只是简单地调用函数.

  public function run()
        {
        self::test ();
          // Rest of code}

关于为什么会导致问题的任何想法?

解决方法:

您正在将字符串而不是可调用字符传递给register_shutdown_function.电话应该是这样的

register_shutdown_function([$this, 'shutDownFunction']);

所以从这里可以看出怎么调用,在CodeIgniter里面要调用Common.php类里面的函数应该为

//这两个函数写在Common.php里,在外部调用时直接
function shutdown_function()
	{
		register_shutdown_function([$this,'_shutdown_handler']);
	}
	
	function errorhandler_function(){
		set_error_handler([$this,'_error_handler']);
	}
$common=new Common();
$common->shutdown_function();
$common->errorhandler_function();

还有一个总办法是直接调用,不用在Common.php写上面两个函数

set_error_handler([$common,'_error_handler']);

register_shutdown_function([$common,'_shutdown_handler']);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值