PHP 之 SplObjectStorage物件儲存
1. 定義
php.net上的定義 The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects.
翻譯: SplObjectStorage類提供從物件到資料對映功能,或者,通過忽視資料來提供物件集合,在很多涉及需要唯一物件的許多情況下,這兩點是十分有用的。
2. 介面說明
class SplObjectStorage implements Countable, Iterator, Serializable, ArrayAccess { //省略,下邊詳細解釋以及翻譯 }
此類實現了 Countable, Iterator, Serializable, ArrayAccess 四個介面,分別對應統計,迭代,序列化和陣列訪問,四個介面分別說明如下
2.1 Countable
此介面中只有一方法 count()
,看 SplObjectStorage
類中此方法的說明(原始碼位置在 php.jar/stubs/SPL/SPL_c1.php檔案的1979行
,可以用phpstorm按住command滑鼠左鍵跳轉過去)
/** * Returns the number of objects in the storage //返回儲存中的物件數量 * @link https://php.net/manual/en/splobjectstorage.count.php * @return int The number of objects in the storage. * @since 5.1.0 */ public function count () {}
翻譯註釋:Returns the number of objects in the storage //返回儲存中的物件數量
2.2 Iterator
介面註釋 Interface for external iterators or objects that can be iterated
的翻譯為 外部迭代器或可以迭代的物件的介面
,此介面中有5個方法分別如下(對應註釋中有翻譯)
/** * Rewind the iterator to the first storage element //將迭代器回到第一個儲存的元素 * @link https://php.net/manual/en/splobjectstorage.rewind.php * @return void * @since 5.1.0 */ public function rewind () {} /** * Returns if the current iterator entry is valid //返回當前迭代器條目是否有效 * @link https://php.net/manual/en/splobjectstorage.valid.php * @return bool true if the iterator entry is valid, false otherwise. * @since 5.1.0 */ public function valid () {} /** * Returns the index at which the iterator currently is//返回當前迭代對應的索引 * @link https://php.net/manual/en/splobjectstorage.key.php * @return int The index corresponding to the position of the iterator. * @since 5.1.0 */ public function key () {} /** * Returns the current storage entry //返回當前儲存的條目 * @link https://php.net/manual/en/splobjectstorage.current.php * @return object The object at the current iterator position. * @since 5.1.0 */ public function current () {} /** * Move to the next entry //移到下一個條目 * @link https://php.net/manual/en/splobjectstorage.next.php * @return void * @since 5.1.0 */ public function next () {}
2.3 Serializable
介面註釋 Interface for customized serializing.
的翻譯為 用於自定義序列化的介面
,此介面中有2個方法分別如下(對應註釋中有翻譯)
/** * Serializes the storage //序列化儲存 * @link https://php.net/manual/en/splobjectstorage.serialize.php * @return string A string representing the storage. //返回表示儲存的字串 * @since 5.2.2 */ public function serialize () {} /** * Unserializes a storage from its string representation //從一個字串表示中對儲存反序列化 * @link https://php.net/manual/en/splobjectstorage.unserialize.php * @param string $serialized <p> * The serialized representation of a storage. * </p> * @return void * @since 5.2.2 */ public function unserialize ($serialized) {}
2.4 ArrayAccess
介面註釋 Interface to provide accessing objects as arrays.
的翻譯為 提供像訪問陣列一樣訪問物件的介面
,此介面中有4個方法分別如下(對應註釋中有翻譯)
/** * Checks whether an object exists in the storage //檢查儲存中是否存在找個物件 * @link https://php.net/manual/en/splobjectstorage.offsetexists.php * @param object $object <p> * The object to look for. * </p> * @return bool true if the object exists in the storage, * and false otherwise. * @since 5.3.0 */ public function offsetExists ($object) {} /** * Associates data to an object in the storage //給儲存中的物件賦值 * @link https://php.net/manual/en/splobjectstorage.offsetset.php * @param object $object <p> * The object to associate data with. * </p> * @param mixed $data [optional] <p> * The data to associate with the object. * </p> * @return void * @since 5.3.0 */ public function offsetSet ($object, $data = null) {} /** * Removes an object from the storage //從儲存中刪除一個物件 * @link https://php.net/manual/en/splobjectstorage.offsetunset.php * @param object $object <p> * The object to remove. * </p> * @return void * @since 5.3.0 */ public function offsetUnset ($object) {} /** * Returns the data associated with an <type>object</type> //從儲存中獲得一個物件 * @link https://php.net/manual/en/splobjectstorage.offsetget.php * @param object $object <p> * The object to look for. * </p> * @return mixed The data previously associated with the object in the storage. * @since 5.3.0 */ public function offsetGet ($object) {}
此介面的功能用程式碼簡單說明如下
$collection = new Supor\Collection();//假設有一Collection類,並且已經實現了ArrayAccess 介面 $collection['a'] = 10;//我們可以像給陣列賦值一樣給此物件賦值 var_dump($collection['a']);//也可以使用取陣列值的方法取得物件的屬性 而不用 '->' //輸出 int(10)
3. 方法說明
在每個方法的註釋中有對應翻譯,來說明這個方法的作用
/** * Adds an object in the storage //向儲存中新增一個物件 * @link https://php.net/manual/en/splobjectstorage.attach.php * @param object $object <p> * The object to add. * </p> * @param mixed $data [optional] <p> * The data to associate with the object. * </p> * @return void * @since 5.1.0 */ public function attach ($object, $data = null) {} /** * Removes an object from the storage //從儲存中刪除一個物件 * @link https://php.net/manual/en/splobjectstorage.detach.php * @param object $object <p> * The object to remove. * </p> * @return void * @since 5.1.0 */ public function detach ($object) {} /** * Checks if the storage contains a specific object //檢查儲存中是否包含特定的物件 * @link https://php.net/manual/en/splobjectstorage.contains.php * @param object $object <p> * The object to look for. * </p> * @return bool true if the object is in the storage, false otherwise. * @since 5.1.0 */ public function contains ($object) {} /** * Adds all objects from another storage //新增一個儲存中所有物件 * @link https://php.net/manual/en/splobjectstorage.addall.php * @param SplObjectStorage $storage <p> * The storage you want to import. * </p> * @return void * @since 5.3.0 */ public function addAll ($storage) {} /** * Removes objects contained in another storage from the current storage //從當前儲存中刪除另一個儲存中包含的物件 * @link https://php.net/manual/en/splobjectstorage.removeall.php * @param SplObjectStorage $storage <p> * The storage containing the elements to remove. * </p> * @return void * @since 5.3.0 */ public function removeAll ($storage) {} /** *從當前儲存中刪除另一個儲存中不包含的物件 * Removes all objects except for those contained in another storage from the current storage * @link https://php.net/manual/en/splobjectstorage.removeallexcept.php * @param SplObjectStorage $storage <p> * The storage containing the elements to retain in the current storage. * </p> * @return void * @since 5.3.6 */ public function removeAllExcept ($storage) {} /** * Returns the data associated with the current iterator entry //返回當前迭代器條目相關的資料 * @link https://php.net/manual/en/splobjectstorage.getinfo.php * @return mixed The data associated with the current iterator position. * @since 5.3.0 */ public function getInfo () {} /** * Sets the data associated with the current iterator entry//設定當前迭代器條目相關的資料 * @link https://php.net/manual/en/splobjectstorage.setinfo.php * @param mixed $data <p> * The data to associate with the current iterator entry. * </p> * @return void * @since 5.3.0 */ public function setInfo ($data) {} /** * Calculate a unique identifier for the contained objects //給包含的物件計算一個唯一ID * @link https://php.net/manual/en/splobjectstorage.gethash.php * @param $object<p> * object whose identifier is to be calculated. * @return string A string with the calculated identifier. * @since 5.4.0 */ public function getHash($object) {}
4. 使用
SplObjectStorage的物件操作
//假設有三個Collection物件 $collection1 = new Supor\Collection(['a' => 'aa', 'b' => 'bb']); $collection2 = new Supor\Collection(['c' => 'cc', 'd' => 'dd']); $collection3 = new Supor\Collection(['e' => 'ee', 'f' => 'ff']); $splStorage = new SplObjectStorage(); $splStorage->attach($collection1); //傳入相同的物件會被替代 $splStorage->attach($collection1); $splStorage->attach($collection2); $splStorage->attach($collection3); //統計$splStorage中有多少個物件 $count = $splStorage->count(); var_dump($count); //得到某一物件的雜湊值 $hash1 = $splStorage->getHash($collection1); var_dump($hash1); //檢查儲存中是否包含$collection3 $contains3 = $splStorage->contains($collection3); var_dump($contains3); //將指標後移 $splStorage->next(); //讀取移動後的key $key = $splStorage->key(); var_dump($key); //刪除某個物件 $splStorage->detach($collection3); //統計刪除後的數量 $count = $splStorage->count(); var_dump($count); //遍歷$splStorage所有物件 //遍歷前先重置一下指標 $splStorage->rewind(); //噹噹前迭代器條目返回真時 while ($splStorage->valid()) { //列印當前條目 var_dump($splStorage->current()); //指標後移 $splStorage->next(); }
程式碼執行結果如下: