11
我写了一个简单的集合类,这样我可以存储我的数组中的对象:PHP array_key_exists()和SPL ArrayAccess接口:不兼容?
class 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));
感谢您的回答;你能提供一个参考吗? – 2009-10-08 14:25:15
我不认为这是写在手册的任何地方,因为它不是一个设计选择。这是一个设计监督,我可能会说一个错误。 PHP语言中有很多不一致的地方。我已经在PHP内部邮件列表中提出了这个功能,并且人们同意我的观点,但是它将会很长时间才会实现。不幸的是我不知道C. – 2009-10-08 14:29:37
以下是该讨论的链接:http://marc.info/?l=php-internals&m=122483924802616&w=2 – 2009-10-08 14:32:22