CachingIterator::__toString

Return the string representation of the current element

Description

public string CachingIterator::__toString()

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.

Parameters

This function has no parameters.

Return Values

The string representation of the current element.

Errors/Exceptions

Throws a BadMethodCallException if none of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT and CachingIterator::TOSTRING_USE_INNER is set.

Examples

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

See Also

  • CachingIterator::__construct
  • CachingIterator::setFlags