Content · Web Development · PHP

PHP NoRewindIterator::current() Method: Usage and Examples

PHP NoRewindIterator::current() Method: Usage and Examples: Reads the current value without automatically advancing to the next element.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

An iterator has a current position and advances only when traversal is requested.

What Is NoRewindIterator::current in PHP?


Reads the current value without automatically advancing to the next element.

NoRewindIterator::current()

This operation does not accept method arguments.

Example 1: Understanding the Basic Behavior


<?php

$iterator = new NoRewindIterator(new ArrayIterator(["north"=>10,"south"=>20,"west"=>30]));
$iterator->rewind();
var_dump($iterator->current());

Output:

int(10)

Explanation: Reads the current value without automatically advancing to the next element.

Example 2: Testing Another Case


<?php

$iterator = new NoRewindIterator(new ArrayIterator(["apple","pear","plum"]));
$iterator->rewind();
var_dump($iterator->current());

Output:

string(5) "apple"

Explanation: current() reads an element at the current cursor and does not move it forward.

Practical Example: Applying the Operation


<?php

$iterator = new NoRewindIterator(new ArrayIterator(["red"=>"ready","blue"=>"waiting","green"=>"done"]));
$iterator->rewind();
var_dump($iterator->current());

Output:

string(5) "ready"

Explanation: Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.

Important Behavior and Edge Cases


  • current() reads an element at the current cursor and does not move it forward.
  • Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.

Comparison With Related Approaches


ApproachWhen to Use It
NoRewindIterator::currentReads the current value without automatically advancing to the next element.
ArrayIteratorArrayIterator wraps an array; iterator decorators can modify traversal without copying the source values.

Common Beginner Mistakes


MistakeCorrect Approach
Reading the current value without checking whether the iterator position is valid.current() reads an element at the current cursor and does not move it forward.
Choosing ArrayIterator without checking whether it provides the same result.ArrayIterator wraps an array; iterator decorators can modify traversal without copying the source values.

Key Takeaways


  • Reads the current value without automatically advancing to the next element.
  • current() reads an element at the current cursor and does not move it forward.
  • Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.

The examples demonstrate where NoRewindIterator::current fits within traversable collections. Verify the relevant preconditions whenever you adapt the code to real data.

Share this resourceSend this page to someone who may find it useful.