CachingIterator::__toString
(PHP 5, PHP 7, PHP 8)
CachingIterator::__toString — Return the string representation of the current element
说明
Gets the string representation of the current element. What is returned depends on the flags of the CachingIterator:
CachingIterator::CALL_TOSTRING
The current element converted to a string. The conversion is done when the iterator moves to the element.
CachingIterator::TOSTRING_USE_KEY
The key of the current element converted to a string.
CachingIterator::TOSTRING_USE_CURRENT
The current element converted to a string. The conversion is done when this method is called.
CachingIterator::TOSTRING_USE_INNER
The inner iterator converted to a string. The conversion is done when the iterator moves to the element, before the inner iterator is moved forward.
参数
此函数没有参数。
返回值
The string representation of the current element.
错误/异常
Throws a BadMethodCallException if none of
CachingIterator::CALL_TOSTRING,
CachingIterator::TOSTRING_USE_KEY,
CachingIterator::TOSTRING_USE_CURRENT and
CachingIterator::TOSTRING_USE_INNER is set.
示例
示例 #1 Using the keys as the string representation
<?php
$iterator = new CachingIterator(
new ArrayIterator(['apple' => 3, 'banana' => 5]),
CachingIterator::TOSTRING_USE_KEY
);
foreach ($iterator as $count) {
echo $iterator, ': ', $count, "\n";
}
?>以上示例会输出:
apple: 3 banana: 5
参见
- CachingIterator::__construct() - Construct a new CachingIterator object for the iterator
- CachingIterator::setFlags() - Set the flags for the CachingIterator object
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
