SplObjectStorage::offsetSet()函數是PHP中的一個內置函數,用於設置存儲對象。
用法:
void SplObjectStorage::offsetSet($obj, $val)
參數:該函數接受上麵提到並在下麵描述的兩個參數:
- $obj:它指定要附加的對象。
- $val:它指定要與對象關聯的值。
返回值:該函數不返回任何值。
以下示例程序旨在說明PHP中的SplObjectStorage::offsetSet()函數:
示例1:
<?php
// Create an empty SplObjectStorage
$str = new SplObjectStorage;
$obj = new StdClass;
// Set offset $obj to $str
$str->offsetSet($obj, "GeeksforGeeks");
// Print Result
var_dump($str->offsetGet($obj));
?>
輸出:
string(13) "GeeksforGeeks"
示例2:
<?php
// Create an Empty SplObjectStorage
$str = new SplObjectStorage();
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
// Attach objects
$str->offsetSet($obj1, "GeksforGeeks");
$str->offsetSet($obj2, "GFG");
$str->offsetSet($obj3);
$str->offsetSet($obj4, "Hello GFG");
// Print result
var_dump($str->offsetGet($obj1));
var_dump($str->offsetGet($obj2));
var_dump($str->offsetGet($obj4));
var_dump($str->offsetGet($obj3));
?>
輸出:
string(12) "GeksforGeeks" string(3) "GFG" string(9) "Hello GFG" NULL
參考: https://www.php.net/manual/en/splobjectstorage.offsetset.php
相關用法
- PHP ArrayIterator offsetSet()用法及代碼示例
- PHP ArrayObject offsetSet()用法及代碼示例
- PHP SplDoublyLinkedList offsetSet()用法及代碼示例
- PHP SplObjectStorage contains()用法及代碼示例
- PHP SplObjectStorage key()用法及代碼示例
- PHP SplObjectStorage next()用法及代碼示例
- PHP SplObjectStorage rewind()用法及代碼示例
- PHP SplObjectStorage removeAll()用法及代碼示例
- PHP SplObjectStorage removeAllExcept()用法及代碼示例
- PHP SplObjectStorage offsetGet()用法及代碼示例
- PHP SplObjectStorage offsetExists()用法及代碼示例
- PHP SplObjectStorage offsetUnset()用法及代碼示例
- PHP SplObjectStorage serialize()用法及代碼示例
- PHP SplObjectStorage setInfo()用法及代碼示例
- PHP SplObjectStorage unserialize()用法及代碼示例
注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | SplObjectStorage offsetSet() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。