1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <?php
class Container implements ArrayAccess { private $className = '';
private $s = array();
public function __construct($className) { if (!class_exists($className)) { throw new Exception('class is not found'); } $this->className = $className; }
public function offsetExists($key) { echo "you're trying to check if something exist" . PHP_EOL; return array_key_exists($key, $this->s); }
public function offsetGet($key) { echo "you're trying to get something" . PHP_EOL; return isset($this->s[$key]) ? $this->s[$key] : ''; }
public function offsetSet($key, $value) { echo "you're trying to set something" . PHP_EOL; var_dump($value,$this->className); if ($value instanceof $this->className) { $this->s[$key] = $value; } else { throw new Exception('非法类型'); } }
public function offsetUnset($key) { echo "you're trying to unset something" . PHP_EOL; unset($this->s[$key]); } }
class cat { public $name = 'nihao'; }
$c = new Container(Cat::class); $nihao = new Cat();
try { $c['asda'] = $nihao; $c['name'] = 'ben'; } catch (Exception $E) { echo $E->getMessage(); echo $E->getLine();
} var_dump($c);
function f(array $peole){
}
|