函式名:SplObjectStorage::getInfo()
適用版本:PHP 5 >= 5.3.0, PHP 7
函式描述:SplObjectStorage::getInfo() 方法用於獲取與物件關聯的額外資訊。
用法:
public mixed SplObjectStorage::getInfo ( object $object )
引數:
$object
:要獲取資訊的物件。
返回值:
- 如果物件存在並與 SplObjectStorage 關聯,則返回與該物件關聯的額外資訊;如果物件不存在或未與 SplObjectStorage 關聯,則返回
null
。
示例:
// 建立一個新的 SplObjectStorage 物件
$storage = new SplObjectStorage();
// 建立幾個物件
$obj1 = new stdClass();
$obj2 = new stdClass();
$obj3 = new stdClass();
// 將物件與額外資訊關聯
$storage->attach($obj1, '資訊1');
$storage->attach($obj2, '資訊2');
// 獲取物件關聯的額外資訊
$info1 = $storage->getInfo($obj1);
$info2 = $storage->getInfo($obj2);
$info3 = $storage->getInfo($obj3);
echo $info1; // 輸出:資訊1
echo $info2; // 輸出:資訊2
var_dump($info3); // 輸出:NULL
在上述示例中,我們首先建立了一個 SplObjectStorage 物件,並使用 attach()
方法將幾個物件與額外資訊關聯。然後,我們使用 getInfo()
方法分別獲取了這些物件關聯的額外資訊。注意,如果物件不存在或未與 SplObjectStorage 關聯,則返回 null
。