I needed to merge SplObjectStorages.
<?php
// As an object set
$SplObjectStorage_1 = new SplObjectStorage();
$object1 = new StdClass;
$object1->attr = 'obj 1';
$object2 = new StdClass;
$object2->attr = 'obj 2';
$object3 = new StdClass;
$object3->attr = 'obj 3';
$SplObjectStorage_1->attach($object1);
$SplObjectStorage_1->attach($object2);
$SplObjectStorage_1->attach($object3);
// Another one object set
$SplObjectStorage_2 = new SplObjectStorage();
$object4 = new StdClass;
$object4->attr = 'obj 4';
$object5 = new StdClass;
$object5->attr = 'obj 5';
$object6 = new StdClass;
$object6->attr = 'obj 6';
$SplObjectStorage_2->attach($object4);
$SplObjectStorage_2->attach($object5);
$SplObjectStorage_2->attach($object6);
/**
* Merge SplObjectStorage
*
* @param how many SplObjectStorage params as you want
* @return SplObjectStorage
*/
function mergeSplObjectStorage() {
$buffer = new SplObjectStorage();
if( func_num_args() > 0 ) {
$args = func_get_args();
foreach ($args as $objectStorage) {
foreach($objectStorage as $object) {
if(is_object( $object ) ) {
$buffer->attach($object);
}
}
}
}
else{
return FALSE;
}
return $buffer;
}
$merge = mergeSplObjectStorage($SplObjectStorage_1, $SplObjectStorage_2);
?>
<?php
echo $merge->count();
?>
Will output :
6
<?php
$merge->rewind();
while($merge->valid()) {
$object = $merge->current();
var_dump($object);
$merge->next();
}
?>
Will ouput :
object(stdClass)#2 (1) {
["attr"]=>
string(5) "obj 1"
}
object(stdClass)#3 (1) {
["attr"]=>
string(5) "obj 2"
}
object(stdClass)#4 (1) {
["attr"]=>
string(5) "obj 3"
}
object(stdClass)#6 (1) {
["attr"]=>
string(5) "obj 4"
}
object(stdClass)#7 (1) {
["attr"]=>
string(5) "obj 5"
}
object(stdClass)#8 (1) {
["attr"]=>
string(5) "obj 6"
}
My two cents.
SplObjectStorage クラス
(PHP 5 >= 5.3.0)
導入
SplObjectStorage クラスは、オブジェクトをデータに対応させたり、 データを渡さずオブジェクトセットとして使用したりします。 これらはどちらも、オブジェクトを一意に特定したい場合に便利です。
クラス概要
/* メソッド */
}例
例1 SplObjectStorage をセットとして使用
<?php
// オブジェクトセット
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$o3 = new StdClass;
$s->attach($o1);
$s->attach($o2);
var_dump($s->contains($o1));
var_dump($s->contains($o2));
var_dump($s->contains($o3));
$s->detach($o2);
var_dump($s->contains($o1));
var_dump($s->contains($o2));
var_dump($s->contains($o3));
?>
上の例の出力は以下となります。
bool(true) bool(true) bool(false) bool(true) bool(false) bool(false)
例2 SplObjectStorage をマップとして使用
<?php
// オブジェクトとデータを対応させます
$s = new SplObjectStorage();
$o1 = new StdClass;
$o2 = new StdClass;
$o3 = new StdClass;
$s[$o1] = "data for object 1";
$s[$o2] = array(1,2,3);
if (isset($s[$o2])) {
var_dump($s[$o2]);
}
?>
上の例の出力は以下となります。
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
目次
- SplObjectStorage::addAll — 別のストレージからすべてのオブジェクトを追加する
- SplObjectStorage::attach — オブジェクトをストレージに追加する
- SplObjectStorage::contains — ストレージに特定のオブジェクトが含まれるかどうかを調べる
- SplObjectStorage::count — ストレージ内のオブジェクトの数を返す
- SplObjectStorage::current — 現在のストレージの要素を返す
- SplObjectStorage::detach — オブジェクトをストレージから取り除く
- SplObjectStorage::getHash — 中に含むオブジェクトの一意な識別子を算出する
- SplObjectStorage::getInfo — イテレータの現在のエントリに関連づけられたデータを返す
- SplObjectStorage::key — イテレータの現在位置を返す
- SplObjectStorage::next — 次のエントリに移動する
- SplObjectStorage::offsetExists — オブジェクトがストレージ内に存在するかどうかを調べる
- SplObjectStorage::offsetGet — オブジェクトに関連づけられたデータを返す
- SplObjectStorage::offsetSet — ストレージ内のオブジェクトにデータを関連づける
- SplObjectStorage::offsetUnset — ストレージからオブジェクトを取り除く
- SplObjectStorage::removeAll — 別のストレージに含まれているオブジェクトを現在のストレージから取り除く
- SplObjectStorage::removeAllExcept — 別のストレージに含まれているもの以外のすべてのオブジェクトを現在のストレージから取り除く
- SplObjectStorage::rewind — イテレータをストレージの最初の要素に巻き戻す
- SplObjectStorage::serialize — ストレージをシリアライズする
- SplObjectStorage::setInfo — イテレータの現在のエントリに関連づけるデータを設定する
- SplObjectStorage::unserialize — ストレージの文字列表現をアンシリアライズする
- SplObjectStorage::valid — イテレータの現在のエントリが有効かどうかを返す
inwebo at gmail dot fr
28-Feb-2012 11:01
Hayley Watson
04-Feb-2012 12:16
As an iterator, SplObjectStorage traverses the _keys_ of the map (the $o1, $o2 of Example 2) rather than the values. To retrieve each of those in turn requires an additional lookup:
<?php
// As a map from objects to data
$s = new SplObjectStorage();
$o1 = (object)array('a'=>1);
$o2 = (object)array('b'=>2);
$o3 = (object)array('c'=>3);
$s[$o1] = "data for object 1";
$s[$o2] = array(1,2,3);
foreach($s as $i => $key)
{
echo "Entry $i:\n"; // You get a numeric index
var_dump($key, $s[$key]);
echo "\n";
}
?>
This makes sense when you use an SplObjectStorage as a set, since in that case all of the "values" are null.
Jan Walther
30-Jun-2011 06:57
I rewrote some scripts and changed object storage with arrays to SplObjectStorage. At some point I needed support of array_rand() but I did not find a function to return a random attached object of an SplObjectStorage object.
So here is my solution for random access to SplObjectStorage:
<?php
$o1 = new StdClass;
$o2 = new StdClass;
$s = new SplObjectStorage;
$s->attach($o1);
$s->attach($o2);
$random = rand(0,$s->count()-1);
$s->rewind();
for($i=0;$i<$random;$i++) {
$s->next();
}
var_dump($s->current());
?>
Robertas at pobox com
31-Jan-2010 11:13
PHP 5.2.x and lower doesn't implement ArrayAccess in SplObjectStorage - it is only implemented starting from PHP 5.3
randallgirard at hotmail dot com
14-Dec-2009 12:01
I have two things to note about SplObjectStorage:
#1: A reference to the object itself is stored (not just a hash to compare against the object) and it must be removed before the object is destroyed and the destructor is executed.
#2: SplObjectStorage::rewind() MUST be called to initiate the iterator and before SplObjectStorage::current() will return an object (and I think the only way to retrieve an object?) rather than automatically starting at the first element as I expected it to, like an array for example. This assumption is based on SplObjectStorage::current() returning NULL until SplObjectStorage::rewind() is called once the objects are contained. As a note, always use REWIND before iterating through or fetching objects.
<?php
class foo {
public function __destruct() {
print("--- DESTRUCTOR FIRED!!<br />\r\n");
}
}
# Create object and storage
$bar = new foo();
$s = new SplObjectStorage();
# Rewind early just as a test
$s->rewind();
# attach the object
$s->attach($bar, array('test'));
# Unset the object; destructor does NOT fire
unset($bar);
print("Object has been unset<br />\r\n");
# First demonstrate that REWIND must be called to initialize the iterator
$obj = $s->current();
var_dump($obj);
print("- Note the NULL (from \$s->current())<br />\r\n");
# Initialize, and then detach the current (and only) object
$s->rewind();
$s->detach( $s->current() );
# The destructor should NOW execute
?>
Output:
Object has been unset
NULL - Note the NULL (from $s->current())
--- DESTRUCTOR FIRED!!
