php __destruct没执行,php 析构方法 __destruct 什么情况下执行

php 析构方法 __destruct 什么情况下执行

求所有的情况,是不是遇到?>php结束符也执行

回复内容:

php 析构方法 __destruct 什么情况下执行

求所有的情况,是不是遇到?>php结束符也执行

RTFM

http://php.net/manual/en/language.oop5.decon.php#object.destruct

析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行

在对象销毁前执行吧?

按照官方手册的描述,析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。代码演示如下

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;

运行结果如下

unset $c2

unset $c1

sf::__destruct

null $c2

null $c1

sf::__destruct

123 $c2

456 $c1

sf::__destruct

the end

析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。

通常来说在脚本结束时(非unset)php才会销毁引用 在脚本结束运行之前运行。

原来遇到 exit();也执行了,蛋疼啊

相关标签:php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值