(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_TOSTRINGThe current element converted to a string. The conversion is done when the iterator moves to the element.
CachingIterator::TOSTRING_USE_KEYThe key of the current element converted to a string.
CachingIterator::TOSTRING_USE_CURRENTThe current element converted to a string. The conversion is done when this method is called.
CachingIterator::TOSTRING_USE_INNERThe inner iterator converted to a string. The conversion is done when the iterator moves to the element, before the inner iterator is moved forward.
This function has no parameters.
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.
Example #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";
}
?>The above example will output:
apple: 3 banana: 5