我有一个包含许多子类的基类,以及一个用于缓存函数结果的通用函数。在缓存函数中,我如何确定调用了哪个子类?
class Base {
public static function getAll() {
return CacheService::cached(function() {
// get objects from the database
});
}
}
class X extends Base {}
class Y extends Base {}
class Z extends Base {}
class CacheService {
function cached($callback) {
list(, $caller) = debug_backtrace();
// $caller['class'] is always Base!
// cannot use get_called_class as it returns CacheService!
// see if function is in cache, otherwise do callback and store results
}
}
X::getAll();
Z::getAll();