CachingIterator::__toString

(PHP 5, PHP 7, PHP 8)

CachingIterator::__toString — Return the string representation of the current element

Description

public function CachingIterator::__toString(): string

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.

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