php 如何销毁一个对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8798443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to destroy an object?
提问by PandemoniumSyndicate
As far as I know (which is very little) , there are two ways, given:
据我所知(很少),有两种方法,给定:
$var = new object()
Then:
然后:
// Method 1: Set to null
$var = null;
// Method 2: Unset
unset($var);
Other better method? Am I splitting hairs here?
其他更好的方法?我在这儿劈头发吗?
回答by Frankie
You're looking for unset()
.
您正在寻找unset()
.
But take into account that you can't explicitly destroy an object.
但请考虑到您不能显式销毁对象。
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 commentsfrom the PHP official manual).
它会留在那里,但是如果您取消设置对象并且您的脚本将 PHP 推到内存限制,则不需要的对象将被垃圾收集。我会选择unset()
(而不是将其设置为 null),因为它似乎具有更好的性能(未经测试,但记录在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.
也就是说,请记住,PHP 总是在页面被提供后立即销毁对象。所以这应该只在非常长的循环和/或密集型页面上需要。
回答by sturrockad
A handy post explaining several mis-understandings about this:
一个方便的帖子解释了对此的几个误解:
Don't Call The Destructor explicitly
This covers several misconceptions about how the destructor works. Calling it explicitly will not actually destroy your variable, according to the PHP5 doc:
这涵盖了关于析构函数如何工作的几个误解。根据 PHP5 文档,显式调用它实际上不会破坏您的变量:
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.
PHP 5 引入了类似于其他面向对象语言(如 C++)的析构函数概念。只要没有其他对特定对象的引用,或在关闭序列期间以任何顺序调用析构函数方法。
The post above does state that setting the variable to null can work in some cases, as long as nothing else is pointing to the allocated memory.
上面的帖子确实说明将变量设置为 null 在某些情况下可以工作,只要没有其他东西指向分配的内存。
回答by Mike Q
Short answer: Both are needed.
简短的回答:两者都需要。
I feel like the right answer was given but minimally. Yeah generally unset() is best for "speed", but if you want to reclaim memory immediately (at the cost of CPU) should want to use null.
我觉得给出了正确的答案,但最低限度。是的,通常 unset() 最适合“速度”,但是如果您想立即回收内存(以 CPU 为代价)应该使用 null。
Like others mentioned, setting to null doesn't mean everything is reclaimed, you can have shared memory (uncloned) objects that will prevent destruction of the object. Moreover, like others have said, you can't "destroy" the objects explicitly anyway so you shouldn't try to do it anyway.
像其他人提到的那样,设置为 null 并不意味着所有内容都被回收,您可以拥有共享内存(未克隆)对象来防止对象被破坏。此外,就像其他人所说的那样,无论如何您都不能明确地“销毁”对象,因此无论如何您都不应该尝试这样做。
You will need to figure out which is best for you. Also you can use __destruct() for an object which will be called on unset or null but it should be used carefully and like others said, never be called directly!
您需要弄清楚哪种方式最适合您。您也可以将 __destruct() 用于将在 unset 或 null 时调用的对象,但应谨慎使用,就像其他人所说的那样,永远不要直接调用!
see:
看:
http://www.stoimen.com/blog/2011/11/14/php-dont-call-the-destructor-explicitly/
http://www.stoimen.com/blog/2011/11/14/php-dont-call-the-destructor-explicitly/
回答by Yevgeniy Afanasyev
This is a simple prove that you cannot destroy an object, you can only destroy a link to it.
这是一个简单的证明,你不能破坏一个对象,你只能破坏一个指向它的链接。
$var = (object)['a'=>1];
$var2 = $var;
$var2->a = 2;
unset($var2);
echo $var->a;
returns
返回
2
2
See it in action here: https://eval.in/1054130
在这里查看它的实际效果:https: //eval.in/1054130
回答by Subrat Kumar Palhar
May be in a situation where you are creating a new mysqli object.
可能处于创建新 mysqli 对象的情况。
$MyConnection = new mysqli($hn, $un, $pw, $db);
$MyConnection = new mysqli($hn, $un, $pw, $db);
but even after you close the object
但即使在你关闭对象之后
$MyConnection->close();
$MyConnection->close();
if you will use print_r()
to check the contents of $MyConnection
, you will get an error as below:
如果您将用于print_r()
检查 的内容$MyConnection
,您将收到如下错误:
Error:
mysqli Object
Warning: print_r(): Property access is not allowed yet in /path/to/program on line ..
( [affected_rows] => [client_info] => [client_version] =>.................)
in which case you can't use unlink()
because unlink()
will require a path name string but in this case $MyConnection
is an Object.
在这种情况下,您不能使用,unlink()
因为unlink()
将需要一个路径名字符串,但在这种情况下$MyConnection
是一个对象。
So you have another choice of setting its value to null:
因此,您可以选择将其值设置为 null:
$MyConnection = null;
$MyConnection = null;
now things go right, as you have expected. You don't have any content inside the variable $MyConnection
as well as you already cleaned up the mysqli Object.
现在一切顺利,正如您所料。您在变量中没有任何内容$MyConnection
,并且您已经清理了 mysqli 对象。
It's a recommended practice to close the Object before setting the value of your variable to null
.
建议的做法是在将变量的值设置为 之前关闭对象null
。
回答by hackartist
I would go with unset because it might give the garbage collector a better hint so that the memory can be available again sooner. Be careful that any things the object points to either have other references or get unset first or you really will have to wait on the garbage collector since there would then be no handles to them.
我会选择 unset ,因为它可能会给垃圾收集器一个更好的提示,以便内存可以更快地再次可用。请注意,对象指向的任何事物要么有其他引用,要么首先被取消设置,否则您真的必须等待垃圾收集器,因为这样就没有它们的句柄了。