I have a number of classes that extend an abstract DatabaseRecord class. Essentially, the DatabaseRecord class handles some common functions that all of the child classes use in interacting with the database (e.g. searching by id, updating, etc.).
Now, I'd like to not have to constantly go to the DB to fetch the record every time, for example, a particular user is referenced on a page load. I had a moderately ingenious idea, such that I could do the following, since PHP has late static binding.
abstract class DatabaseRecord{
static protected $cachedRecords;
public static function searchById($id){
if(!isset(static::$cachedRecords[$id])) {
// logic
static::$cachedRecords[$id] = static::constructFromDatabase($results);
}
return static::$cachedRecords[$id];
}
// ... more logic
}
Unfortunately, all the child classes share the same static $cachedRecords
.
I could fix this by redeclaring static protected $cachedRecords;
in all the child classes and declaring the $cachedRecords
in DatabaseRecord to be private
to stop me from forgetting the redeclaration, but this seems an inelegant solution.
Is there a better way of doing this so it's write-once-and-forget?