php 析构方法 destruct 什么情况下执行 求所有的情况,是不是遇到?>php结束符也执行
析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行
在对象销毁前执行吧?
按照官方手册的描述,析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。代码演示如下
class sf{ public function destruct()
{ echo METHOD . PHP_EOL;
}
}
$c1 = new sf;
$c2 = $c1;echo 'unset $c2' . PHP_EOL;unset($c2);echo 'unset $c1' . PHP_EOL;unset($c1);// ----$c1 = new sf;
$c2 = $c1;echo 'null $c2' . PHP_EOL;
$c2 = null;echo 'null $c1' . PHP_EOL;
$c1 = null;// ----$c1 = new sf;
$c2 = $c1;echo '123 $c2' . PHP_EOL;
$c2 = 123;echo '456 $c1' . PHP_EOL;
$c1 = 456;echo 'the end' . PHP_EOL;运行结果如下
立即学习“PHP免费学习笔记(深入)”;
unset $c2unset $c1 sf::destructnull $c2null $c1 sf::destruct123 $c2456 $c1 sf::destruct the end
析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。
通常来说在脚本结束时(非unset)php才会销毁引用 在脚本结束运行之前运行。
以上就是php:析构方法 __destruct在什么情况下执行?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号