class Demo {
static private $db = null;
static private $cache = [];
static protected function getDb() {
if(self::$db === null) {
self::$db = ... // init $db
}
return self::$db;
}
static protected function cachedAction($arg) {
if(!array_key_exist($arg, self::$cache)) {
self::$cache[$arg] = ... // do action
}
return self::$cache[$arg];
}
}
class DemoChild : public Demo {
}
$a = new Demo();
$b = new DemoChild();
$a->getDb()->query();
$b->getDb()->query(); // uses the same connection
$c = $a->cachedAction('one');
$d = $b->cachedAction('one'); // no action, cache used
ПРАКТИЧЕСКОЕ отличие