SplObjectStorage::offsetExists()函數是PHP中的一個內置函數,用於檢查對象是否存在於存儲中。
用法:
bool SplObjectStorage::offsetExists($object)
參數:該函數接受單個參數$object,該參數指定要檢查的對象。
返回值:如果對象存在於存儲中,則此函數返回true,否則返回false。
以下示例程序旨在說明PHP中的SplObjectStorage::offsetExists()函數:
程序1:
<?php
// Create an empty SplObjectStorage
$str = new SplObjectStorage;
$obj = new StdClass;
// Attch $obj to $str
$str->attach($obj);
// Print Result
var_dump($str->offsetExists($obj));
?>
輸出:
bool(true)
程序2:
<?php
// Create an Empty SplObjectStorage
$str = new SplObjectStorage();
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
// Attach only three objects
$str->attach($obj1, "GeksforGeeks");
$str->attach($obj2, "GFG");
$str->attach($obj3);
// Print result
var_dump($str->offsetExists($obj1));
var_dump($str->offsetExists($obj2));
var_dump($str->offsetExists($obj4));
var_dump($str->offsetExists($obj3));
?>
輸出:
bool(true) bool(true) bool(false) bool(true)
參考: https://www.php.net/manual/en/splobjectstorage.offsetexists.php
相關用法
- PHP ArrayObject offsetExists()用法及代碼示例
- PHP SplDoublyLinkedList offsetExists()用法及代碼示例
- PHP SplFixedArray offsetExists()用法及代碼示例
- PHP ArrayIterator offsetExists()用法及代碼示例
- PHP SplObjectStorage key()用法及代碼示例
- PHP SplObjectStorage next()用法及代碼示例
- PHP SplObjectStorage contains()用法及代碼示例
- PHP SplObjectStorage removeAll()用法及代碼示例
- PHP SplObjectStorage offsetUnset()用法及代碼示例
- PHP SplObjectStorage attach()用法及代碼示例
- PHP SplObjectStorage setInfo()用法及代碼示例
- PHP SplObjectStorage serialize()用法及代碼示例
- PHP SplObjectStorage removeAllExcept()用法及代碼示例
- PHP SplObjectStorage rewind()用法及代碼示例
- PHP SplObjectStorage unserialize()用法及代碼示例
注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | SplObjectStorage offsetExists() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。