函式名稱:SplObjectStorage::next()
適用版本:PHP 5.3.0 及以上版本
函式描述:SplObjectStorage::next() 方法用於將當前指標移動到下一個元素。
用法:
void SplObjectStorage::next ( void )
示例:
// 建立一個 SplObjectStorage 物件
$storage = new SplObjectStorage();
// 建立幾個物件
$obj1 = new stdClass();
$obj2 = new stdClass();
$obj3 = new stdClass();
// 將物件新增到 SplObjectStorage 中
$storage->attach($obj1);
$storage->attach($obj2);
$storage->attach($obj3);
// 設定當前指標為第一個元素
$storage->rewind();
// 輸出第一個元素
echo $storage->current() . "\n"; // 輸出: [object] stdClass
// 將指標移到下一個元素
$storage->next();
// 輸出下一個元素
echo $storage->current() . "\n"; // 輸出: [object] stdClass
// 將指標移到下一個元素
$storage->next();
// 輸出下一個元素
echo $storage->current() . "\n"; // 輸出: [object] stdClass
注意:在示例中,我們建立了一個 SplObjectStorage 物件,並新增了三個物件。然後,我們使用 rewind() 方法將指標設定為第一個元素,並使用 current() 方法輸出當前元素。接著,我們使用 next() 方法將指標移動到下一個元素,並再次使用 current() 方法輸出當前元素。重複此操作,直到遍歷完所有元素。