Почему класс, реализующий ArrayAccess, Iterator и Countable, не работает с array_filter ( )?

У меня есть следующий класс:

<?php

/*
* Abstract class that, when subclassed, allows an instance to be used as an array.
* Interfaces `Countable` and `Iterator` are necessary for functionality such as `foreach`
*/
abstract class AArray implements ArrayAccess, Iterator, Countable
{
    private $container = array();

    public function offsetSet($offset, $value) 
    {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }

    public function offsetExists($offset) 
    {
        return isset($this->container[$offset]);
    }

    public function offsetUnset($offset) 
    {
        unset($this->container[$offset]);
    }

    public function offsetGet($offset) 
    {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }

    public function rewind() {
            reset($this->container);
    }

    public function current() {
            return current($this->container);
    }

    public function key() {
            return key($this->container);
    }

    public function next() {
            return next($this->container);
    }

    public function valid() {
            return $this->current() !== false;
    }   

    public function count() {
     return count($this->container);
    }

}

?>

Затем у меня есть другой класс, который подклассов AArray:

<?php

require_once 'AArray.inc';

class GalleryCollection extends AArray { }

?>

Когда я заполняю экземпляр GalleryCollection с помощью данные, а затем попробуйте использовать его в array_filter () , в первом аргументе я получаю следующую ошибку:

Warning: array_filter() [function.array-filter]: The first argument should be an array in
7
задан Benjamin 8 July 2019 в 14:52
поделиться

1 ответ

Потому что array_filter работает только с массивами.

Рассмотрите другие варианты, например FilterIterator, или сначала создайте массив из вашего объекта.

9
ответ дан 7 December 2019 в 03:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: