是否有没有在PHP中没有调用析构函数的情况? [英] Are there any instances when the destructor in PHP is NOT called?

查看:176
本文介绍了是否有没有在PHP中没有调用析构函数的情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次发布到stackoverflow,但是我这些线程对我有很大帮助!

This is my first time posting to stackoverflow, but I these threads have helped me tremendously!

任何人,我的问题...在PHP中没有调用析构函数的情况吗?我问的原因是因为我有一个将数据映射到对象的映射器类,在构造函数中,我启动了一个事务,在析构函数中,我将调用一个commit(我还将有一个成员函数,该函数也可以执行committal (如有必要).如果有任何情况不调用析构函数的情况,我想知道,这样我就可以预料到它的发生并进行适当的计划.

Anywho, onto my question... are there any instances when the destructor in PHP is NOT called? The reason I ask is because I have a mapper class which maps data to objects and in the constructor, I start a transaction and in the destructor I'll call a commit (I'll also have a member function which can also do the committal, if necessary). If there are any instances when the destructor isn't called, I'd like to know so I can anticipate it happening and plan appropriately.

非常感谢!

推荐答案

  • 根据手册,析构函数即使脚本使用die()exit()终止,也会被执行:

    • According to the manual, destructors are executed even if the script gets terminated using die() or exit():

      即使使用exit()停止脚本执行,也会调用析构函数.在析构函数中调用exit()将阻止其余的关闭例程执行.

      The destructor will be called even if script execution is stopped using exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing.

    • 根据此SO问题,析构函数会<达到PHP的执行时间限制时,strong> not 不会执行(在Apache 2上确认,在Windows 7上为PHP 5.2).

    • According to this SO question, the destructor does not get executed when PHP's execution time limit is reached (Confirmed on Apache 2, PHP 5.2 on Windows 7).

      当脚本由于达到内存限制而终止时,析构函数也不会被执行. (刚刚测试)

      The destructor also does not get executed when the script terminates because the memory limit was reached. (Just tested)

      析构函数在致命错误上执行(刚刚经过测试)更新:OP无法确认这一点-情况不同时似乎存在致命错误

      The destructor does get executed on fatal errors (Just tested) Update: The OP can't confirm this - there seem to be fatal errors where things are different

      它不会不会在解析错误时执行(因为不会解释整个脚本)

      It does not get executed on parse errors (because the whole script won't be interpreted)

      如果服务器进程崩溃或发生PHP无法控制的其他异常,则肯定不会执行析构函数.

      The destructor will certainly not be executed if the server process crashes or some other exception out of PHP's control occurs.

      总而言之,它看起来非常可靠.

      All in all, it looks pretty reliable.

      在析构函数中执行除清理以外的其他操作的缺点是,您的选择受到一定限制.您不能再抛出异常(除非您在析构函数中再次捕获到异常),不能输出任何错误消息,不能再真正依赖其他对象(例如数据库接口)的存在. ....我在使用析构函数方面没有丰富的经验,但是我不确定您打算做的事是否可行.

      The downside of doing things other than cleanup in the destructor, though, is that your options there are somewhat limited. You can't throw exceptions any more (except if you catch them again inside the destructor), you can't output any error messages, you can't really rely on the presence of other objects (like the database interface) any more ..... I don't have deep experience in working with destructors but I'm not sure whether what you're planning to do is a feasible idea.

      这篇关于是否有没有在PHP中没有调用析构函数的情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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