查詢

SplObjectStorage::count()函式—用法及示例

「 獲取 SplObjectStorage 物件中儲存的物件數量 」


函式名稱:SplObjectStorage::count()

適用版本:PHP 5 >= 5.1.0, PHP 7

函式描述:SplObjectStorage::count() 函式用於獲取 SplObjectStorage 物件中儲存的物件數量。

語法:int SplObjectStorage::count ( void )

引數:無引數

返回值:返回一個整數,表示 SplObjectStorage 物件中的物件數量。

示例:

// 建立一個 SplObjectStorage 物件
$storage = new SplObjectStorage();

// 建立兩個物件
$obj1 = new stdClass();
$obj2 = new stdClass();

// 將物件新增到 SplObjectStorage 物件中
$storage->attach($obj1);
$storage->attach($obj2);

// 獲取 SplObjectStorage 物件中的物件數量
$count = $storage->count();

// 輸出物件數量
echo "物件數量:" . $count;  // 輸出:物件數量:2

在上述示例中,我們首先建立了一個 SplObjectStorage 物件 $storage。然後,我們建立了兩個 stdClass 物件 $obj1$obj2。接下來,我們使用 attach() 方法將這兩個物件新增到 $storage 物件中。最後,我們使用 count() 方法獲取 $storage 物件中的物件數量,並將其儲存在變數 $count 中。最後,我們輸出了物件數量,結果為 2。

補充糾錯
熱門PHP函式
分享連結