I have a base class with many sub-classes, and a generic function to cache the results of a function. In the cache function, how do I figure out what sub-class was called?
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();