大网数据广告活动
PHP 语言与运行手册

CachingIterator::__construct · Construct a new CachingIterator object for the iterator

· 52CE 我爱测

CachingIterator::__construct — Construct a new CachingIterator object for the iterator

CachingIterator::__construct

(PHP 5, PHP 7, PHP 8)

CachingIterator::__construct — Construct a new CachingIterator object for the iterator

Description

public function CachingIterator::__construct(Iterator $iterator, int $flags = CachingIterator::CALL_TOSTRING)

Creates a CachingIterator that iterates over iterator, staying one element ahead of it.

Parameters

iterator

The iterator to cache.

flags

A bitmask of the CachingIterator constants.

At most one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT and CachingIterator::TOSTRING_USE_INNER may be given. It selects what CachingIterator::__toString() returns; if none of them is given, calling it throws an exception.

CachingIterator::FULL_CACHE keeps every element that has been iterated over, so that it can be accessed with array syntax or retrieved with CachingIterator::getCache().

Changelog

Version Description
8.0.0 Now throws a ValueError if flags contains more than one of the string conversion flags; previously, an InvalidArgumentException was thrown.

Notes

Caution

With CachingIterator::CALL_TOSTRING, which is the default, each element is converted to a string as soon as the iterator moves to it, whether or not CachingIterator::__toString() is ever called. An element that is an array raises an E_WARNING, and an object that does not implement __toString() throws an Error. To iterate over such elements, pass flags that do not contain CachingIterator::CALL_TOSTRING, such as 0.

See Also

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。