Arrayaccess和本机php数组函数

时间:2012-01-09 11:53:04

标签: 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

1 个答案:

答案 0 :(得分: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 …
}