什么时候需要使用后期静态绑定? [英] When would you need to use late static binding?

查看:57
本文介绍了什么时候需要使用后期静态绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读此描述后期静态绑定(LSB)我很清楚地知道发生了什么.现在,在哪种情况下可能最有用或最需要?

After reading this description of late static binding (LSB) I see pretty clearly what is going on. Now, under which sorts of circumstances might that be most useful or needed?

推荐答案

在以下情况下,我需要使用LSB:

I needed LSB this for the following scenario:

  • 想象一下您正在构建一个邮件处理器"守护程序,该守护程序从电子邮件服务器下载消息,对其进行分类,解析,保存,然后根据消息的类型执行某些操作.
  • 类层次结构:您有一个基本的Message类,带有子级"BouncedMessage"和"AcceptedMessage".
  • 每种消息类型都有自己的方式将自身持久保存在磁盘上.例如,所有BouncedMessage类型的消息都尝试将其自身另存为BouncedMessage-id.xml.另一方面,AcceptedMessage需要以不同的方式保存自身-作为AcceptedMessage-timestamp.xml.这里重要的是,用于确定文件名模式的逻辑对于不同的子类而言是不同的,但是对于子类中的所有项都是 shared (共享).这就是为什么采用静态方法才有意义.
  • 基本消息类具有抽象的静态方法(是的,抽象的 AND 静态)保存". BouncedMessage使用具体的静态方法实现此方法.然后,在实际检索消息的类中,您可以调用":: save()"
  • Imagine you're building a "mail processor" daemon that downloads the message from an email server, classifies it, parses it, saves it, and then does something, depending on the type of the message.
  • Class hierarchy: you have a base Message class, with children "BouncedMessage" and "AcceptedMessage".
  • Each of the message types has its own way to persist itself on disk. For example, all messages of type BouncedMessage try to save itself as BouncedMessage-id.xml. AcceptedMessage, on the other hand, needs to save itself differently - as AcceptedMessage-timestamp.xml. The important thing here is that the logic for determining the filename pattern is different for different subclasses, but shared for all items within the subclass. That's why it makes sense for it to be in a static method.
  • Base Message class has an abstract static method (yes, abstract AND static) "save". BouncedMessage implements this method with a concrete static method. Then, inside the class that actually retrieves the message, you can call "::save()"

如果您想了解更多有关该主题的信息:

If you want to learn more about the subject:

  • http://www.qcodo.com/forums/topic.php/2356
  • http://community.livejournal.com/php/585907.html
  • http://bugs.php.net/bug.php?id=42681

这篇关于什么时候需要使用后期静态绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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