就地用其他对象替换对象
假设我想构建一个数据库抽象层,它使用延迟加载机制。
如果我要求该层加载根对象,它会加载其外部表示并构建自身。
然后它以某种方式识别出某些链接对象的存在。由于预先加载所有内容可能成本高昂,因此它为相关对象建立了代理。这样的代理应该能够被传递。
如果在这样的代理上调用第一条消息,它将加载其外部表示并构造自身。由于对代理的引用可能已被传递,因此创建的对象需要就地替换现有的代理对象。
我可以在 PHP 中用另一个对象就地替换一个对象吗?
Suppose I'd like to build a database abstraction layer, which uses a lazy loading mechanism.
If I ask the layer to load a root object, it loads its external representation and constructs itself.
It then somehow identifies that certain linked objects exist. Since it might be costly to load all up-front, it established proxies for related objects. Such proxies should be able to get passed around.
If a first message is called on such a proxy, it loads its external representation and constructs itself. Since references to the proxy may have been passed around, the created object needs to replace the existing proxy-object in-place.
Can I in-place replace an object with another object in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不相信一个对象可以用另一个对象替换对自身的所有引用。相反,让代理对象使用重载。在基本代理对象(例如名为
OOProxy
)上实现代理,然后将其扩展为延迟加载代理对象的LazyProxy
类。只要您不需要检查对象的类型,任何引用代理的对象都无法将其与代理区分开来。I don't believe it's possible for an object to replace all references to itself with another object. Instead, have your proxy objects forward property access and method invocation using overloading. Implement proxying on a base proxy object (named e.g.
OOProxy
), then extend this to aLazyProxy
class that lazily loads the proxied object. As long as you don't need to examine the type of the object, anything that has a reference to the proxy won't be able to distinguish it from the proxied.