note 107720 added to class.splobjectstorage
| From: | inwebo at gmail dot fr | Date: | Tue, 28 Feb 2012 23:01:03 +0000 |
| Subject: | note 107720 added to class.splobjectstorage | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186421@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 79.88.166.179
----
Manual Page -- http://www.php.net/manual/en/class.splobjectstorage.php
Edit -- https://master.php.net/note/edit/107720
Del: integrated -- https://master.php.net/note/delete/107720/integrated
Del: useless -- https://master.php.net/note/delete/107720/useless
Del: bad code -- https://master.php.net/note/delete/107720/bad+code
Del: spam -- https://master.php.net/note/delete/107720/spam
Del: non-english -- https://master.php.net/note/delete/107720/non-english
Del: in docs -- https://master.php.net/note/delete/107720/in+docs
Del: other reasons-- https://master.php.net/note/delete/107720
Reject -- https://master.php.net/note/reject/107720
Search -- https://master.php.net/manage/user-notes.php