如何在 PHP 中销毁一个类对象?

发布于 2024-10-31 04:49:03 字数 289 浏览 1 评论 0原文

我编写了一个用于存储全局变量/函数的小类。我的问题是 - 脚本完成后是否有必要销毁类对象? 或者 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深已缘浅 2024-11-07 04:49:03

如果脚本完成,则释放内存。你已经准备好了:)

If the script finishes, the memory is released. You're ready as is :)

温暖的光 2024-11-07 04:49:03

不,您不需要自己销毁任何变量(对象就是变量):一旦您的 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.

┈┾☆殇 2024-11-07 04:49:03

正如@Nanne所说,如果脚本完成,内存就会被释放,但是在某些情况下,您可能需要 取消设置($web); .

As @Nanne sayd, if the script finished the memory is released, however in some circumstances you might whant to unset($web); .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文