Kristian
Kristian

Reputation: 3461

Arrayaccess and native php array functions

Is there any way to use array_merge(), array_pop(), .. functions to work with ArrayAccess?

Since now i've tried Iterate interface and __set_state() magic method with no success.

Error that is given: array_replace_recursive() [<a href='function.array-replace-recursive'>function.array-replace-recursive</a>]: Argument #1 is not an array.

Just fo a record, gettype() returns object and is_array() returns false and i'm usin php version 5.3.8

Upvotes: 17

Views: 3197

Answers (1)

Gordon
Gordon

Reputation: 317137

Unfortunately, no. They only work with the native array type. You have to add those as methods to your object's public API and implement them there, e.g. something like this:

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 …
}

Upvotes: 15

Related Questions