Php析构函数 [英] Php Destructors

查看:115
本文介绍了Php析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您不得不在类中使用__destruct时,请给我一些现实生活中的例子.

Please give me some real life examples when you had to use __destruct in your classes.

推荐答案

好吧,由于我的最后一个答案显然没有达到目标,所以让我再试一次.互联网上有很多关于此主题的资源和示例.进行一些搜索和浏览其他框架的代码,您将看到一些非常好的示例...

Ok, since my last answer apparently didn't hit the mark, let me try this again. There are plenty of resources and examples on the internet for this topic. Doing a bit of searching and browsing other framework's code and you'll see some pretty good examples...

别忘了,仅仅因为PHP会在终止时为您关闭资源并不意味着在您不再需要它们时显式关闭它们(或者最好不要关闭它们)是很不好的……这取决于用例(是一直使用到最后,还是很早就有一个调用,然后在其余的执行中不再需要)...

Don't forget that just because PHP will close resources on termination for you doesn't mean that it's bad to explictly close them when you no longer need them (or good to not close them)... It depends on the use case (is it being used right up to the end, or is there one call early on and then not needed again for the rest of execution)...

现在,我们知道对象被销毁时会调用__destruct.从逻辑上讲,如果对象被破坏会发生什么?好吧,这意味着它不再可用.因此,如果它有开放的资源,那么在销毁这些资源时关闭这些资源是否有意义?当然,在一般的网页中,网页很快就会终止,因此让PHP关闭它们通常并不可怕.但是,如果脚本由于某种原因长时间运行会怎样?然后,您有资源泄漏.那么,为什么不关闭不再需要的所有内容(或者在不再可用的情况下考虑析构函数的范围)呢?

Now, we know that __destruct is called when the object is destroyed. Logically, what happens if the object is destroyed? Well, it means it's no longer available. So if it has resources open, doesn't it make sense to close those resources as it's being destroyed? Sure, in the average web page, the page is going to terminate shortly after, so letting PHP close them usually isn't terrible. However, what happens if for some reason the script is long-running? Then you have a resource leak. So why not just close everything when you no longer need it (or considering the scope of the destructor, when it's no longer available)?

以下是现实世界框架中的一些示例:

Here's some examples in real world frameworks:

  1. 锂的锂\ net \ Socket类
  2. Kohana的Memcached驱动程序
  3. Joomla的FTP实现
  4. Zend Framework的SMTP邮件传输类
  5. CodeIgniter的TTemplate类
  6. 蛋糕的整洁过滤器助手
  7. 有关使用析构函数的Google-Groups线程Symfony会话课程
  1. Lithium's lithium\net\Socket class
  2. Kohana's Memcached Driver
  3. Joomla's FTP Implementation
  4. Zend Frameworks's SMTP Mail Transport Class
  5. CodeIgniter's TTemplate Class
  6. A Tidy Filter Helper for Cake
  7. A Google-Groups Thread about using Destructors For the Symfony Session Class

有趣的是,Kohana会跟踪标签,以便以后可以通过命名空间"删除它(而不是仅清除缓存).因此,它使用析构函数将这些更改刷新到硬存储中.

The interesting thing is that Kohana keeps track of the tags, so that it can delete by "namespace" later (instead of just clearing the cache). So it uses the destructor to flush those changes to the hard storage.

CodeIgniter类还做一些有趣的事情,因为它将调试输出添加到析构函数中的输出流中.我并不是说这很好,但这只是另一个用途的例子...

The CodeIgniter class also does something interesting in that it adds debugging output to the output stream in the destructor. I'm not saying this is good, but it's an example of yet another use...

每当我在主控制器上运行很长时间的进程时,我都会亲自使用析构函数.在构造函数中,我检查pid文件.如果该文件存在(并且其进程仍在运行),则抛出异常.如果没有,我将使用当前进程ID创建一个文件.然后,在析构函数中删除该文件.因此,与其说是释放资源,不如说是在自我清理之后.

I personally use destructors whenever I have long running processes on my master controller. In the constructor, I check for a pid file. If that file exists (And its process is still running), I throw an exception. If not, I create a file with the current processes id. Then, in the destructor I remove that file. So it's more about cleaning up after itself than just freeing resources...

这篇关于Php析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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