123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- --TEST--
- SPL: SplObjectStorage with accociatied information
- --FILE--
- <?php
- class TestClass
- {
- public $test = 25;
- public function __construct($test = 42)
- {
- $this->test = $test;
- }
- }
- class MyStorage extends SplObjectStorage
- {
- public $bla = 25;
- public function __construct($bla = 26)
- {
- $this->bla = $bla;
- }
- }
- $storage = new MyStorage();
- foreach(array(1=>"foo",2=>42) as $key => $value)
- {
- $storage->attach(new TestClass($key), $value);
- }
- var_dump(count($storage));
- foreach($storage as $object)
- {
- var_dump($object->test);
- }
- var_dump($storage);
- var_dump(serialize($storage));
- echo "===UNSERIALIZE===\n";
- $storage2 = unserialize(serialize($storage));
- var_dump(count($storage2));
- foreach($storage2 as $object)
- {
- var_dump($object->test);
- }
- var_dump($storage2);
- $storage->attach(new TestClass(3), new stdClass);
- $storage->attach(new TestClass(4), new TestClass(5));
- echo "===UNSERIALIZE2===\n";
- var_dump(unserialize(serialize($storage)));
- $storage->rewind();
- $storage->next();
- var_dump($storage->key());
- var_dump($storage->current());
- var_dump($storage->getInfo());
- $storage->setInfo("bar");
- var_dump($storage->getInfo());
- echo "===UNSERIALIZE3===\n";
- var_dump(unserialize(serialize($storage)));
- $storage->rewind();
- $storage->next();
- $storage->next();
- var_dump($storage->key());
- var_dump($storage->current());
- $storage->attach($storage->current(), "replaced");
- echo "===UNSERIALIZE4===\n";
- var_dump(unserialize(serialize($storage)));
- ?>
- --EXPECTF--
- int(2)
- int(1)
- int(2)
- object(MyStorage)#%d (2) {
- ["bla"]=>
- int(26)
- ["storage":"SplObjectStorage":private]=>
- array(2) {
- [0]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(1)
- }
- ["inf"]=>
- string(3) "foo"
- }
- [1]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(2)
- }
- ["inf"]=>
- int(42)
- }
- }
- }
- string(%d) "%s"
- ===UNSERIALIZE===
- int(2)
- int(1)
- int(2)
- object(MyStorage)#%d (2) {
- ["bla"]=>
- int(26)
- ["storage":"SplObjectStorage":private]=>
- array(2) {
- [0]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(1)
- }
- ["inf"]=>
- string(3) "foo"
- }
- [1]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(2)
- }
- ["inf"]=>
- int(42)
- }
- }
- }
- ===UNSERIALIZE2===
- object(MyStorage)#%d (2) {
- ["bla"]=>
- int(26)
- ["storage":"SplObjectStorage":private]=>
- array(4) {
- [0]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(1)
- }
- ["inf"]=>
- string(3) "foo"
- }
- [1]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(2)
- }
- ["inf"]=>
- int(42)
- }
- [2]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(3)
- }
- ["inf"]=>
- object(stdClass)#%d (0) {
- }
- }
- [3]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(4)
- }
- ["inf"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(5)
- }
- }
- }
- }
- int(1)
- object(TestClass)#%d (1) {
- ["test"]=>
- int(2)
- }
- int(42)
- string(3) "bar"
- ===UNSERIALIZE3===
- object(MyStorage)#%d (2) {
- ["bla"]=>
- int(26)
- ["storage":"SplObjectStorage":private]=>
- array(4) {
- [0]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(1)
- }
- ["inf"]=>
- string(3) "foo"
- }
- [1]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(2)
- }
- ["inf"]=>
- string(3) "bar"
- }
- [2]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(3)
- }
- ["inf"]=>
- object(stdClass)#%d (0) {
- }
- }
- [3]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(4)
- }
- ["inf"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(5)
- }
- }
- }
- }
- int(2)
- object(TestClass)#7 (1) {
- ["test"]=>
- int(3)
- }
- ===UNSERIALIZE4===
- object(MyStorage)#%d (2) {
- ["bla"]=>
- int(26)
- ["storage":"SplObjectStorage":private]=>
- array(4) {
- [0]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(1)
- }
- ["inf"]=>
- string(3) "foo"
- }
- [1]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(2)
- }
- ["inf"]=>
- string(3) "bar"
- }
- [2]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(3)
- }
- ["inf"]=>
- string(8) "replaced"
- }
- [3]=>
- array(2) {
- ["obj"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(4)
- }
- ["inf"]=>
- object(TestClass)#%d (1) {
- ["test"]=>
- int(5)
- }
- }
- }
- }
|