2012-01-09 1 views
11

array_merge(), array_pop(), .. ArrayAccess와 함께 작동하는 데 사용할 수있는 방법이 있습니까?Arrayaccess 및 기본 PHP 배열 기능

이제는 Iterate 인터페이스와 __set_state() 마술 방법을 사용해 보았습니다.

주어진 오류 : array_replace_recursive() [<a href='function.array-replace-recursive'>function.array-replace-recursive</a>]: Argument #1 is not an array. 그냥 기록 FO

, gettype() 반환 objectis_array() 반환 false와 나는 아니, 불행하게도 PHP 버전 5.3.8

답변

8

을저기서하고 있습니다. 그들은 원시 배열 유형으로 만 작동합니다. 그것들을 객체의 공개 API에 메소드로 추가하고 거기에 구현해야합니다. 다음과 같은 내용이 있습니다.

class YourClass implements ArrayAccess, Countable 
{ 
    public function pop() 
    { 
     $lastOffset = $this->count() - 1; 
     $lastElement = $this->offsetGet($lastOffset); 
     $this->offsetUnset($lastOffset); 

     return $lastElement; 
    } 

    public function mergeArray(array $array) { 
     // implement the logic you want 
    } 

    // other code … 
} 
+0

생각해 보면 좋습니다. Ty – Kristian

+0

이 대답을 이해할 수 없다 - 당신이'array_merge'와 다른 원시 배열 함수를'ArrayAccess' 구현 클래스의 메소드로 구현해야한다고 말하는 것입니까? – alexw

+0

@alexw 예.이 함수는 배열에 입력을 요구하기 때문에 가능합니다. ArrayAccess를 구현하는 클래스는 배열과 동일한 유형이 아닙니다. – Gordon