gpt4 book ai didi

php - 什么时候不会在 PHP 中调用 __destruct?

转载 作者:IT王子 更新时间:2023-10-29 00:42:34 24 4
gpt4 key购买 nike

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

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

$obj = new MyDestructableClass();

当上面的脚本处于复杂环境时,__destruct不会在exit时被调用,但我无法重现很容易。有没有人注意到这一点?

编辑

我把所有的东西都贴在这里,这是symfony的测试环境,这意味着如果你熟悉这个框架,你可以很容易地重现它:

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
  • 如果代码中某处出现 fatal error
  • 如果另一个析构函数抛出异常
  • 如果您尝试在析构函数中处理异常 (PHP >= 5.3.0)

我现在能想到的就这么多了

& Pascal MARTIN 所说的。这是调试的第一步。

关于php - 什么时候不会在 PHP 中调用 __destruct?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2385047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com