[英]Why does a class that implements ArrayAccess, Iterator, and Countable not work with array_filter()?
I have the following class: 我有以下课程:
<?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);
}
}
?>
Then, I have another class that sub-classes AArray: 然后,我有另一个子类AArray的类:
<?php
require_once 'AArray.inc';
class GalleryCollection extends AArray { }
?>
When I fill a GalleryCollection instance with data and then try to use it in array_filter()
, in the first argument, I get the following error: 当我用数据填充GalleryCollection实例然后尝试在
array_filter()
使用它时,在第一个参数中,我收到以下错误:
Warning: array_filter() [function.array-filter]: The first argument should be an array in
Because array_filter
only works with arrays. 因为
array_filter
仅适用于数组。
Look at other options, like FilterIterator
, or create an array from your object first. 查看其他选项,如
FilterIterator
,或者首先从对象创建数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.