何时在PHP中不调用__destruct? [英] When will __destruct not be called in PHP?

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

问题描述

class MyDestructableClass {
   function __construct() {
       print "\nIn constructor\n";
       $this->name = "MyDestructableClass";
   }

   function __destruct() {
       print "\nDestroying " . $this->name . "\n";
   }
}

$obj = new MyDestructableClass();

当以上脚本处于 complex 环境中时,当exit时不会调用__destruct,但是我无法轻松重现.有人注意到了吗?

When the above script is in a complex environment,the __destruct won't get called when exit,but I can't reproduce it easily.Have someone ever noticed this ?

编辑

我将在此处发布所有内容,这是symfony的测试环境,这意味着如果您熟悉框架,则可以轻松地重现它:

I'll post the whole stuff here,it's the testing environment of symfony,which means you can easily reproduce it if you are familar with the framework:

require_once dirname(__FILE__).'/../bootstrap/Doctrine.php';


$profiler = new Doctrine_Connection_Profiler();

$conn = Doctrine_Manager::connection();
$conn->setListener($profiler);

$t = new lime_test(0, new lime_output_color());

class MyDestructableClass {
   function __construct() {
       print "\nIn constructor\n";
       $this->name = "MyDestructableClass";
   }

   function __destruct() {
       print "\nDestroying " . $this->name . "\n";
   }
}

$obj = new MyDestructableClass();
$news = new News();

$news->setUrl('http://test');
$news->setHash('http://test');
$news->setTitle('http://test');
$news->setSummarize('http://test');
$news->setAccountId(1);
$news->setCategoryId(1);
$news->setThumbnail('http://test');
$news->setCreatedAt(date('Y-m-d H:i:s',time()));
$news->setUpdatedAt(date('Y-m-d H:i:s',time()));
$news->save();
exit();

推荐答案

__destruct称为:

  • 如果exit在另一个析构函数中被调用
  • 取决于PHP版本:如果在注册到register_shutdown_function的关闭函数中调用exit
  • 如果代码中出现致命错误
  • 如果另一个析构函数抛出异常
  • 如果您尝试在析构函数中处理异常(PHP> = 5.3.0)
  • If exit is called in another destructor
  • Depending on the PHP Version: if exit is called in a shutdown function registered with register_shutdown_function
  • If there is a fatal error somewhere in the code
  • If another destructor throws an exception
  • If you try to handle an exception in a destructor (PHP >= 5.3.0)

猜猜这就是我现在能想到的一切

Guess that's all I can think of right now

&帕斯卡尔·马丁(Pascal MARTIN)说了什么.这是调试的第一步.

& What Pascal MARTIN said. That's the first step of debugging that.

这篇关于何时在PHP中不调用__destruct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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