SplObjectStorage::key

イテレータの現在位置を返す

説明

public int SplObjectStorage::key()

イテレータの現在位置を返します。

パラメータ

この関数にはパラメータはありません。

戻り値

イテレータの現在位置に対応するインデックスを返します。

例1 SplObjectStorage::key の例

<?php
$s 
= new SplObjectStorage();

$o1 = new stdClass;
$o2 = new stdClass;

$s->attach($o1"d1");
$s->attach($o2"d2");

$s->rewind();
while(
$s->valid()) {
    
$index  $s->key();
    
$object $s->current(); // current($s) と同等

    
var_dump($index);
    
var_dump($object);
    
$s->next();
}
?>

上の例の出力は、 たとえば以下のようになります。

int(0)
object(stdClass)#2 (0) {
}
int(1)
object(stdClass)#3 (0) {
}

参考

  • SplObjectStorage::rewind
  • SplObjectStorage::current
  • SplObjectStorage::next
  • SplObjectStorage::valid