如何在 PHP 中销毁一个类对象?
我编写了一个用于存储全局变量/函数的小类。我的问题是 - 脚本完成后是否有必要销毁类对象? 或者 PHP 会销毁该对象本身吗?
这是我的代码:
$web=new c_web("myWeb");
$web->loadTemplate("/!framework/admin/template.htm");
$web->doStuff();
// script has finished - destroying required here?
如果我需要销毁它,我该怎么做?
I wrote a little class for storing global variables/functions. My question is - is it necessary to destroy the class object after the script has finished?
or will PHP destroy that object itself?
Here's my code:
$web=new c_web("myWeb");
$web->loadTemplate("/!framework/admin/template.htm");
$web->doStuff();
// script has finished - destroying required here?
In case I need to destroy it, how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果脚本完成,则释放内存。你已经准备好了:)
If the script finishes, the memory is released. You're ready as is :)
不,您不需要自己销毁任何变量(对象就是变量):一旦您的 PHP 脚本到达末尾,它的变量将被释放,并且相应的内存也将被释放。
实际上,当变量的 变量结束时,变量会自动销毁已达到作用域——并且,当到达脚本末尾时,即是该脚本执行引入的作用域的末尾。
*(Answering a comment to another answer)*
Of course, when your script is ended because of an error, the same thing happens : the variables are freed, and the memory released.
No, you do not need to destroy any variable yourself (and an object is a variable) : as soon as your PHP script reaches its end, its variables will be freed, and the correspondig memory released.
Actually, a variable gets destroyed automatically when the end of its variable's scope is reached -- and, when you reach end of script, it's the end of the scope introduced by that script's execution.
*(Answering a comment to another answer)*
Of course, when your script is ended because of an error, the same thing happens : the variables are freed, and the memory released.
正如@Nanne所说,如果脚本完成,内存就会被释放,但是在某些情况下,您可能需要 取消设置($web); .
As @Nanne sayd, if the script finished the memory is released, however in some circumstances you might whant to unset($web); .