當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP SplObjectStorage current()用法及代碼示例


SplObjectStorage::current()函數是PHP中的內置函數,用於獲取存儲的當前條目。

用法:

object SplObjectStorage::current()

參數:該函數不接受任何參數。


返回值:此函數返回當前存儲的對象。

以下程序說明了PHP中的SplObjectStorage::current()函數:

示例1:

<?php 
  
// Declare an SplObjectStorage 
$storage = new SplObjectStorage(); 
  
// Declare new object 
$obj = new StdClass; 
  
// Use attach() function to add object 
$storage->attach($obj, "GeeksforGeeks"); 
  
$storage->rewind(); 
  
// Use current() function to get 
// the current object 
$object = $storage->current();  
$data = $storage->getInfo(); 
  
var_dump($object); 
var_dump($data); 
?>
輸出:
object(stdClass)#2 (0) {
}
string(13) "GeeksforGeeks"

示例2:

<?php 
  
// Declare an SplObjectStorage 
$str = new SplObjectStorage(); 
  
// Declare new object 
$obj1 = new StdClass; 
$obj2 = new StdClass; 
$obj3 = new StdClass; 
$obj4 = new StdClass; 
  
// Use attach() function to add object 
$str->attach($obj1, "GeeksforGeeks"); 
$str->attach($obj2, "GFG"); 
$str->attach($obj3, "Geeks"); 
$str->attach($obj4, "PHP"); 
  
$str->rewind(); 
  
while($str->valid()) { 
    $index = $str->key(); 
      
    // Use current() function to get 
    // the current object 
    $object = current($str);  
    $data = $str->getInfo(); 
  
    var_dump($object); 
    var_dump($data); 
    $str->next(); 
} 
?>
輸出:
bool(false)
string(13) "GeeksforGeeks"
bool(false)
string(3) "GFG"
bool(false)
string(5) "Geeks"
bool(false)
string(3) "PHP"

參考: https://www.php.net/manual/en/splobjectstorage.current.php



相關用法


注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | SplObjectStorage current() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。