PHP __destruct导致net :: ERR_CONNECTION_RESET register_shutdown_function不是。有什么不同? [英] PHP __destruct is causing net::ERR_CONNECTION_RESET register_shutdown_function is not. What's the difference?

查看:125
本文介绍了PHP __destruct导致net :: ERR_CONNECTION_RESET register_shutdown_function不是。有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下问题。
我有一个会话类,应在请求执行结束时将其数据保存到数据库中。基本上,当它被破坏时。
在这种情况下,我使用的是单例模式。
我有一个这样的析构函数:

so I have following problem. I have a session class that should save it's data to database at the end of the request execution. Basically, when it is destructed. I'm using singleton pattern in this case. I have a destructor like this:

public function __destruct()
{
    $this->_save(); // _save is public
    // exit('I can reach this point with no error');
}

但是使用该代码,我从chrome和其他浏览器中获得了net :: ERR_CONNECTION_RESET。
如果我注释掉析构函数并将其放在构造函数中:

But with that code I get net::ERR_CONNECTION_RESET from chrome and other browsers. If I comment out the destructor and place this in constructor:

register_shutdown_function(array($this, '_save'));

当我直接调用_save方法时,它不会返回任何异常。

The _save method is not returning any Exceptions when I call it directly.

一切正常。

谢谢!

推荐答案

好,我找到了解决方案。 _save()方法正在调用一些将数据保存到数据库的方法。但!数据库实例是SINGLETON,因此任何地方都没有引用,并且此时数据库对象已被破坏。解决方案是将对数据库实例的引用保存在模型中的某个位置。似乎register_shutdown_function正在工作,因为还没有破坏任何东西。

Ok, I found the solution. The _save() method was calling some methods that were saving data to database. BUT! The database instance is a SINGLETON, so there were no references anywhere and the database object was already destroyed at this point. The solution was to save the reference to database instance somewhere in the model. Looks like register_shutdown_function was working because nothing was destroyed yet.

这篇关于PHP __destruct导致net :: ERR_CONNECTION_RESET register_shutdown_function不是。有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆