Arrayaccess和本机php数组函数

Kri*_*ian 13 php arrays arrayaccess

有没有办法使用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

Gor*_*don 9

很不幸的是,不行.它们仅适用于本机数组类型.您必须将这些方法添加到对象的公共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 …
}
Run Code Online (Sandbox Code Playgroud)