我写了一个简单的集合类,以便我可以将数组存储在对象中:
原文链接:https://www.f2er.com/php/132381.htmlclass App_Collection implements ArrayAccess,IteratorAggregate,Countable { public $data = array(); public function count() { return count($this->data); } public function offsetExists($offset) { return (isset($this->data[$offset])); } public function offsetGet($offset) { if ($this->offsetExists($offset)) { return $this->data[$offset]; } return false; } public function offsetSet($offset,$value) { if ($offset) { $this->data[$offset] = $value; } else { $this->data[] = $value; } } public function offsetUnset($offset) { unset($this->data[$offset]); } public function getIterator() { return new ArrayIterator($this->data); } }
问题:当调用此对象上的array_key_exists()时,它始终返回“false”,因为它似乎该函数未被SPL处理.有没有办法解决?
概念证明:
$collection = new App_Collection(); $collection['foo'] = 'bar'; // EXPECTED return value: bool(true) // REAL return value: bool(false) var_dump(array_key_exists('foo',$collection));