Arrayaccess和本机php数组函数


Arrayaccess and native php array functions

有什么方法可以使用array_merge()array_pop()。。使用ArrayAccess的函数?

从现在起,我尝试了Iterate接口和__set_state()魔术方法,但都没有成功。

给出的错误:array_replace_recursive() [<a href='function.array-replace-recursive'>function.array-replace-recursive</a>]: Argument #1 is not an array

仅作为记录,gettype()返回objectis_array()返回false,我使用php版本5.3.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 …
}