如何销毁一个对象? [英] How to destroy an object?

查看:36
本文介绍了如何销毁一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知(很少),有两种方法,给定:

As far as I know (which is very little) , there are two ways, given:

$var = new object()

那么:

// Method 1: Set to null
$var = null;
// Method 2: Unset 
unset($var); 

其他更好的方法?我是不是在这里分心了?

Other better method? Am I splitting hairs here?

推荐答案

您正在寻找 unset().

You're looking for unset().

但要考虑到您不能显式销毁对象.

它会留在那里,但是如果您取消设置对象并且您的脚本将 PHP 推送到内存限制,则不需要的对象将被垃圾收集.我会使用 unset()(而不是将其设置为 null),因为它似乎具有更好的性能(未经测试但记录在 PHP 官方手册中的评论之一).

It will stay there, however if you unset the object and your script pushes PHP to the memory limits the objects not needed will be garbage collected. I would go with unset() (as opposed to setting it to null) as it seems to have better performance (not tested but documented on one of the comments from the PHP official manual).

也就是说,请记住,PHP 总是在页面被提供后立即销毁对象.因此,只有在非常长的循环和/或密集型页面上才需要这样做.

That said, do keep in mind that PHP always destroys the objects as soon as the page is served. So this should only be needed on really long loops and/or heavy intensive pages.

这篇关于如何销毁一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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