函式名稱:SplObjectStorage::addAll()
適用版本:PHP 5 >= 5.3.0, PHP 7
函式說明:SplObjectStorage::addAll() 函式將另一個 SplObjectStorage 物件中的所有物件新增到當前的 SplObjectStorage 物件中。
語法:public void SplObjectStorage::addAll(SplObjectStorage $storage)
引數:
- $storage:要新增到當前 SplObjectStorage 物件的另一個 SplObjectStorage 物件。
返回值:該函式沒有返回值。
示例:
// 建立兩個 SplObjectStorage 物件
$storage1 = new SplObjectStorage();
$storage2 = new SplObjectStorage();
// 新增一些物件到 storage1
$obj1 = new stdClass();
$obj2 = new stdClass();
$obj3 = new stdClass();
$storage1->attach($obj1);
$storage1->attach($obj2);
// 新增一些物件到 storage2
$obj4 = new stdClass();
$obj5 = new stdClass();
$storage2->attach($obj3);
$storage2->attach($obj4);
$storage2->attach($obj5);
// 使用 addAll() 函式將 storage2 中的物件新增到 storage1 中
$storage1->addAll($storage2);
// 遍歷 storage1,輸出所有物件
foreach ($storage1 as $obj) {
echo get_class($obj) . "\n";
}
輸出:
stdClass
stdClass
stdClass
stdClass
stdClass
在上面的示例中,我們建立了兩個 SplObjectStorage 物件 $storage1 和 $storage2,並向它們分別新增了一些物件。然後,我們使用 SplObjectStorage::addAll() 函式將 $storage2 中的所有物件新增到 $storage1 中。最後,我們遍歷 $storage1 並輸出所有物件的類名。可以看到,$storage1 中現在包含了 $storage2 中的所有物件。