fork download
  1. <?php
  2. class AbstractPhoto {
  3. public $width = 260;
  4. public $height = 210;
  5. public $method = 'crop';
  6. public $type = 'unknown';
  7. public $image = 'nophoto.jpg';
  8. public function __construct($image) {
  9. $this->type = strtolower(get_class($this));
  10. }
  11. }
  12.  
  13. class Photos extends AbstractPhoto {
  14. public function __construct($image) {
  15. parent::__construct($image);
  16. $this->image = $image;
  17. }
  18. }
  19.  
  20. class Catalog extends AbstractPhoto {
  21. }
  22.  
  23. class PseudoArray implements ArrayAccess {
  24. private $classTable = array (false => 'Photos', true => 'Catalog');
  25. public function offsetGet ($offset) {
  26. $class = $this->classTable[strlen($offset)>0];
  27. return new $class($offset);
  28. }
  29. public function offsetExists ($offset) {return true;}
  30. public function offsetSet ($offset, $value) {}
  31. public function offsetUnset ($offset) {}
  32. }
  33.  
  34.  
  35. $properties = new PseudoArray();
  36.  
  37. var_dump($properties['']);
  38.  
  39. var_dump($properties['ololo.jpg']);
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
object(Photos)#2 (5) {
  ["width"]=>
  int(260)
  ["height"]=>
  int(210)
  ["method"]=>
  string(4) "crop"
  ["type"]=>
  string(6) "photos"
  ["image"]=>
  string(0) ""
}
object(Catalog)#2 (5) {
  ["width"]=>
  int(260)
  ["height"]=>
  int(210)
  ["method"]=>
  string(4) "crop"
  ["type"]=>
  string(7) "catalog"
  ["image"]=>
  string(11) "nophoto.jpg"
}