函式名稱:SplObjectStorage::current()
適用版本:PHP 5 >= 5.3.0, PHP 7
函式描述:獲取當前迭代器指向的物件
用法: SplObjectStorage::current ( void ) : object
引數:無
返回值:返回當前迭代器指向的物件
示例:
// 建立一個SplObjectStorage物件
$storage = new SplObjectStorage();
// 新增一些物件到SplObjectStorage
$obj1 = new stdClass();
$obj2 = new stdClass();
$obj3 = new stdClass();
$storage->attach($obj1, "Data for obj1");
$storage->attach($obj2, "Data for obj2");
$storage->attach($obj3, "Data for obj3");
// 設定迭代器指向第一個物件
$storage->rewind();
// 獲取當前迭代器指向的物件
$currentObject = $storage->current();
// 輸出當前物件的資料
echo $storage->getInfo(); // 輸出 "Data for obj1"
// 移動迭代器到下一個物件
$storage->next();
// 獲取當前迭代器指向的物件
$currentObject = $storage->current();
// 輸出當前物件的資料
echo $storage->getInfo(); // 輸出 "Data for obj2"
以上示例演示了SplObjectStorage::current()函式的用法。首先,我們建立一個SplObjectStorage物件並將一些物件附加到它上面。然後,我們使用rewind()函式將迭代器指向第一個物件。接著,使用current()函式獲取當前迭代器指向的物件,並透過getInfo()函式獲取該物件的相關資料。然後,使用next()函式將迭代器移動到下一個物件,並再次使用current()函式獲取當前迭代器指向的物件。最後,透過getInfo()函式輸出該物件的相關資料。