Pergunta

Eu escrevi uma aula de coleção simples para que eu possa armazenar minhas matrizes em objetos:

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);
    }
}

Problema: Ao chamar Array_Key_Exists () neste objeto, ele sempre retorna "false", pois parece que essa função não está sendo tratada pelo SPL. Existe alguma maneira de contornar isso?

Prova de conceito:

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

Solução

Este é um problema conhecido que poderia ser abordado no PHP6. Até então, use isset() ou ArrayAccess::offsetExists().

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top