函式名稱:SplObjectStorage::removeAll()
適用版本:PHP 5 >= 5.3.0, PHP 7
函式描述:該函式用於從SplObjectStorage物件中移除所有的物件。
用法:
void SplObjectStorage::removeAll ( SplObjectStorage $storage )
引數:
- $storage:要從中移除物件的SplObjectStorage物件。
返回值:該函式沒有返回值。
示例:
// 建立一個SplObjectStorage物件
$storage = new SplObjectStorage();
// 建立幾個物件
$obj1 = new stdClass();
$obj2 = new stdClass();
// 將物件新增到SplObjectStorage物件中
$storage->attach($obj1);
$storage->attach($obj2);
// 列印儲存的物件數量
echo "儲存的物件數量:" . $storage->count() . "\n"; // 輸出:儲存的物件數量:2
// 從SplObjectStorage物件中移除所有物件
$storage->removeAll($storage);
// 列印移除後的物件數量
echo "移除後的物件數量:" . $storage->count() . "\n"; // 輸出:移除後的物件數量:0
以上示例中,我們首先建立了一個SplObjectStorage物件,並建立了兩個stdClass物件$obj1和$obj2。然後,我們將這兩個物件附加到SplObjectStorage物件中,並使用count()函式列印儲存的物件數量,結果為2。
接下來,我們使用removeAll()函式從SplObjectStorage物件中移除所有的物件。最後,我們再次使用count()函式列印移除後的物件數量,結果為0,表明所有物件都被成功移除。