在PHP中,destruct函数是一个特殊的魔术方法,用于在对象被销毁时自动调用。可以通过在类中定义一个名为__destruct()的函数来使用destruct函数。
示例代码如下:
class MyClass {
public function __construct() {
echo 'Object created';
}
public function __destruct() {
echo 'Object destroyed';
}
}
$obj = new MyClass(); // 输出 'Object created'
unset($obj); // 输出 'Object destroyed'
在上面的示例中,当创建一个MyClass对象时,__construct()方法会被调用以输出’Object created’。当使用unset()函数销毁对象时,__destruct()方法会被调用以输出’Object destroyed’。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: php定义全局变量要注意什么