fork(1) download
  1. <?php
  2. class Base {
  3. private static $cache = array();
  4.  
  5. public function &__get($name) {
  6. if ($name != 'cache') {
  7. // error handling
  8. }
  9.  
  10. $type = get_class($this);
  11. if (!isset(self::$cache[$type])) {
  12. self::$cache[$type] = array();
  13. }
  14.  
  15. return self::$cache[$type];
  16. }
  17. }
  18.  
  19. class Derived extends Base {
  20. }
  21.  
  22. $b = new Base;
  23. $b2 = new Base;
  24. $d = new Derived;
  25. $b->cache['foo'] = 42;
  26.  
  27. echo $b->cache['foo']."\n"; // 42
  28. echo $b2->cache['foo']."\n"; // also 42
  29. echo $d->cache['foo']."\n"; // nothing (actually, undefined index); cache is per-class
  30.  
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
42
42