# Class yii\web\SessionIterator

Inheritance | yii\web\SessionIterator
Implements | Iterator
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/web/SessionIterator.php

SessionIterator implements an iterator for traversing session variables managed by yii\web\Session.

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__construct() | Constructor. | yii\web\SessionIterator
current() | Returns the current array element. | yii\web\SessionIterator
key() | Returns the key of the current array element. | yii\web\SessionIterator
next() | Moves the internal pointer to the next array element. | yii\web\SessionIterator
rewind() | Rewinds internal array pointer. | yii\web\SessionIterator
valid() | Returns whether there is an element at current position. | yii\web\SessionIterator

## Method Details

Hide inherited methods

__construct() public method

Constructor.

public void __construct ( )

Source code

                    public function __construct()
    {
        $this->_keys = array_keys(isset($_SESSION) ? $_SESSION : []);
        $this->rewind();
    }

current() public method

Returns the current array element.

This method is required by the interface Iterator.

public mixed current ( )
return | mixed | The current array element

Source code

                    #[\ReturnTypeWillChange]
    public function current()
    {
        return $this->_key !== false && isset($_SESSION[$this->_key]) ? $_SESSION[$this->_key] : null;
    }

key() public method

Returns the key of the current array element.

This method is required by the interface Iterator.

public string|integer|null key ( )
return | string|integer|null | The key of the current array element

Source code

                    #[\ReturnTypeWillChange]
    public function key()
    {
        return $this->_key === false ? null : $this->_key;
    }

next() public method

Moves the internal pointer to the next array element.

This method is required by the interface Iterator.

public void next ( )

Source code

                    #[\ReturnTypeWillChange]
    public function next()
    {
        do {
            $this->_key = next($this->_keys);
        } while ($this->_key !== false && !isset($_SESSION[$this->_key]));
    }

rewind() public method

Rewinds internal array pointer.

This method is required by the interface Iterator.

public void rewind ( )

Source code

                    #[\ReturnTypeWillChange]
    public function rewind()
    {
        $this->_key = reset($this->_keys);
    }

valid() public method

Returns whether there is an element at current position.

This method is required by the interface Iterator.

public boolean valid ( )

Source code

                    #[\ReturnTypeWillChange]
    public function valid()
    {
        return $this->_key !== false;
    }