我希望能够通过使用构造函数而不是某些工厂方法来使用对象的透明(穷人)缓存。
$a = new aClass();应该检查该对象是否存在于缓存中,如果不存在,则创建它并将其添加到缓存中。
一些伪代码:
class aClass {
public function __construct($someId) {
if (is_cached($someId) {
$this = get_cached($someId);
} else {
// do stuff here
set_cached($someId, $this);
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这是不可能的,因为你不能$this在 php 中重新定义。
有什么建议么?