【问题标题】:Fatal error: Cannot declare class ProductFilterIterator, because the name is already in use in致命错误:无法声明类 ProductFilterIterator,因为该名称已在
【发布时间】:2020-03-20 04:29:06
【问题描述】:

借助 ProductFilterIterator 类想要获取数据

class ProductFilterIterator extends \FilterIterator
{
    protected $filter;
    protected $length;

    public function __construct(\Iterator $iterator,  $filter)
    {
        $this->filter = $filter;
        $this->length = -strlen($filter);
        parent::__construct($iterator);
    }

    public function accept() : bool
    {
        $current = $this->getInnerIterator()->current();
        return substr($current['Товар'], $this->length) == $this->filter;
    }
}


$iterator = (new \ArrayObject($obj['items']))->getIterator();
$filter = new ProductFilterIterator($iterator, $arPart['ARTICLE']);

foreach ($filter as $obj) {
   echo "<pre>";

我收到一个错误 - 致命错误:无法声明类 ProductFilterIterator,因为该名称已在使用中

【问题讨论】:

标签: php function class php-7 filter-iterator


【解决方案1】:

尝试使用命名空间,

namespace Custom;
class ProductFilterIterator extends \FilterIterator
{
    protected $filter;
    protected $length;

    public function __construct(\Iterator $iterator,  $filter)
    {
        $this->filter = $filter;
        $this->length = -\strlen($filter);
        parent::__construct($iterator);
    }

    public function accept() : bool
    {
        $current = $this->getInnerIterator()->current();
        return \substr($current['Товар'], $this->length) == $this->filter;
    }
}


$iterator = (new \ArrayObject($obj['items']))->getIterator();
$filter = new ProductFilterIterator($iterator, $arPart['ARTICLE']);

foreach ($filter as $obj) {
   echo "<pre>";

【讨论】:

    猜你喜欢
    • 2016-11-03
    • 2020-10-10
    • 2023-03-10
    • 2019-12-25
    • 2019-04-15
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多