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類提供從對象到數據映射功能,或者,經過忽視數據來提供對象集合,在不少涉及須要惟一對象的許多狀況下,這兩點是十分有用的。php

SplObjectStorage類實現了對象存儲映射表,應用於須要惟一標識多個對象的存儲場景。 在PHP5.3.0以前僅能存儲對象,以後能夠針對每一個對象添加一條對應的數據。 SplObjectStorage類的數據存儲依賴於PHP的HashTable實現,與傳統的使用數組和spl_object_hash函數生成數組key相比, 其直接使用HashTable的實如今性能上有較大的優點。 有一些奇怪的是,在PHP手冊中,SplObjectStorage類放在數據結構目錄下。 可是他的實現和觀察者模式的接口放在同一個文件(ext/spl/spl_observer.c)。 實際上他們並無直接的關係。《深刻理解php內核》

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 //返回存儲中的對象數量phpstorm

2.2 Iterator

接口註釋Interface for external iterators or objects that can be iterated 的翻譯爲外部迭代器或能夠迭代的對象的接口,此接口中有5個方法分別以下(對應註釋中有翻譯)ide

/**
 * 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) {}

此接口的功能用代碼簡單說明以下spa

$collection = new Supor\Collection();//假設有一Collection類,而且已經實現了ArrayAccess 接口
$collection['a'] = 10;//咱們能夠像給數組賦值同樣給此對象賦值
var_dump($collection['a']);//也可使用取數組值的方法取得對象的屬性 而不用 '->'
//輸出 int(10)

3. 方法說明

在每一個方法的註釋中有對應翻譯,來講明這個方法的做用.net

/**
 * 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();
}

代碼執行結果以下:
執行結果

相關文章
相關標籤/搜索