|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-12-19 10:31 UTC] digital1kk at interia dot pl
Description:
------------
If SplFixedArray object is created via unserialize() of serialized instance then the getSize() and count() methods return 0 instead of expected actual size.
Tested on Debian Squeeze 6 64bit:
x86-5.4.0RC3-dev
Tested on windows 7 64bit:
x86-5.4.0RC3-nts
x86-5.4.0trunk(12.12)-nts
x86-5.4.0snap(13.12)-nts
x86-5.3.8-nts
x86-5.3.6-ts
x86-5.3.6-nts
x64-5.3.6-nts
x86-5.3.4-nts
x86-5.3.1-ts
Test script:
---------------
<?php
$a = new SplFixedArray(2);
$a[0] = 1;
$a[1] = 2;
echo '$a = ', var_dump($a);
echo 'Sizeof $a = ' . $a->getSize(), PHP_EOL;
echo 'Count $a = ' . $a->count(), PHP_EOL;
echo PHP_EOL;
$b = unserialize(serialize($a));
echo '$b = ', var_dump($b);
echo 'Sizeof $b = ' . $b->getSize(), PHP_EOL;
echo 'Count $b = ' . $b->count(), PHP_EOL;
Expected result:
----------------
$a = object(SplFixedArray)#1 (2) {
[0]=>
int(1)
[1]=>
int(2)
}
Sizeof $a = 2
Count $a = 2
$b = object(SplFixedArray)#2 (2) {
[0]=>
int(1)
[1]=>
int(2)
}
Sizeof $b = 2
Count $b = 2
Actual result:
--------------
$a = object(SplFixedArray)#1 (2) {
[0]=>
int(1)
[1]=>
int(2)
}
Sizeof $a = 2
Count $a = 2
$b = object(SplFixedArray)#2 (2) {
[0]=>
int(1)
[1]=>
int(2)
}
Sizeof $b = 0
Count $b = 0
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
Quick fix is to store in serialized form internal array: ------------------------------------- $sa = serialize($a->toArray()); $ua = unserialize($sa); $b = SplFixedArray::fromArray($ua); var_dump($b); echo 'Sizeof $b = ' . $b->getSize(), PHP_EOL; echo 'Count $b = ' . $b->count(), PHP_EOL; ------------------------------------- Gives the expected results Also I forgot in php >= 5.4.0RC3 (should I report this as separate bug?) The actual result of var_dump($b) is: $b = object(SplFixedArray)#2 (2) { ["0"]=> int(1) ["1"]=> int(2) } The keys are strings and not integers and this causes ------------------------------------- $b = unserialize(serialize($a)); SplFixedArray::fromArray($b->toarray()); ------------------------------------- To throw an exception 'InvalidArgumentException' with message 'array must contain only positive integer keys'