开发者

PHP array_key_exists() and SPL ArrayAccess interface: not compatible?

开发者 https://www.devze.com 2022-12-08 14:55 出处:网络
I wrote a simple collection class so that I can store my arrays in objects: class App_Collecti开发者_如何学运维on implements ArrayAccess, IteratorAggregate, Countable

I wrote a simple collection class so that I can store my arrays in objects:

class App_Collecti开发者_如何学运维on 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);
    }
}

Problem: when calling array_key_exists() on this object, it always returns "false" as it seems this function is not being handled by the SPL. Is there any way around this?

Proof of concept:

$collection = new App_Collection();
$collection['foo'] = 'bar';
// EXPECTED return value: bool(true) 
// REAL return value: bool(false) 
var_dump(array_key_exists('foo', $collection));


This is a known issue which might be addressed in PHP6. Until then, use isset() or ArrayAccess::offsetExists().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号