php array_key_exists () 및 SPL ArrayAccess 인터페이스 : 호환되지 않습니까?

StackOverflow https://stackoverflow.com/questions/1538124

  •  20-09-2019
  •  | 
  •  

문제

배열을 개체에 저장할 수 있도록 간단한 컬렉션 클래스를 썼습니다.

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 ()를 호출 할 때이 함수는 SPL에 의해 처리되지 않는 것처럼 항상 "false"를 반환합니다. 이 주위에 어떤 방법이 있습니까?

개념의 증거:

$collection = new App_Collection();
$collection['foo'] = 'bar';
// EXPECTED return value: bool(true) 
// REAL return value: bool(false) 
var_dump(array_key_exists('foo', $collection));
도움이 되었습니까?

해결책

이것은 알려진 문제입니다 ~할 것 같다 PHP6에서 해결해야합니다. 그때까지 사용하십시오 isset() 또는 ArrayAccess::offsetExists().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top