SplObjectStorage::detach()函數是PHP中的內置函數,用於從存儲中刪除對象。
用法:
void SplObjectStorage::detach($obj)
參數:該函數接受單個參數$obj,該參數指定要從存儲中刪除的對象。
返回值:該函數不返回任何值。
以下程序說明了PHP中的SplObjectStorage::detach()函數:
程序1:
<?php
// Creating class
$obj = new StdClass;
// Create an empty storage class
$str = new SplObjectStorage();
// Add some object
$str->attach($obj, "GeeksforGeeks");
// Print result before detaching
var_dump(count($str));
// Detaching object
$str->detach($obj);
// Print result after detach
var_dump(count($str));
?>
輸出:
int(1) int(0)
程序2:
<?php
// Creating class
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
// Create an empty storage class
$str = new SplObjectStorage();
// Add some object
$str->attach($obj1, "GeeksforGeeks");
$str->attach($obj2);
$str->attach($obj3, "GFG");
// Print result before detaching
var_dump(count($str));
// Detaching object
$str->detach($obj1);
// Print result after detach first object
var_dump(count($str));
// Detaching object
$str->detach($obj3);
// Print result after detach second object
var_dump(count($str));
?>
輸出:
int(3) int(2) int(1)
參考: https://www.php.net/manual/en/splobjectstorage.detach.php
相關用法
- PHP SplObjectStorage next()用法及代碼示例
- PHP SplObjectStorage key()用法及代碼示例
- PHP SplObjectStorage contains()用法及代碼示例
- PHP SplObjectStorage attach()用法及代碼示例
- PHP SplObjectStorage addAll()用法及代碼示例
- PHP SplObjectStorage valid()用法及代碼示例
- PHP SplObjectStorage offsetUnset()用法及代碼示例
- PHP SplObjectStorage current()用法及代碼示例
- PHP SplObjectStorage getinfo()用法及代碼示例
- PHP SplObjectStorage offsetSet()用法及代碼示例
- PHP SplObjectStorage offsetExists()用法及代碼示例
- PHP SplObjectStorage offsetGet()用法及代碼示例
- PHP SplObjectStorage count()用法及代碼示例
- PHP SplObjectStorage unserialize()用法及代碼示例
- PHP SplObjectStorage setInfo()用法及代碼示例
注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | SplObjectStorage detach() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。