Bug #80788 [NEW]: __unserialize type hinting conflict with ArrayObject::unserialize
| From: | hello at rayfung dot hk | Date: | Tue, 23 Feb 2021 06:53:31 +0000 |
| Subject: | Bug #80788 [NEW]: __unserialize type hinting conflict with ArrayObject::unserialize | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-232334@lists.php.net to get a copy of this message | ||
From: hello at rayfung dot hk
Operating system: MacOS Apple Silicon
PHP version: 7.4.15
Package: SPL related
Bug Type: Bug
Bug description:__unserialize type hinting conflict with ArrayObject::unserialize
Description:
------------
If you have a new class extends an ArrayObject, it will display a
warning if you implement the __unserialize method with the type hinting
of the first parameter:
Warning: Declaration of Collection::__unserialize(array $data): void
should be compatible with ArrayObject::__unserialize($serialized)
Test script:
---------------
class Collection extends \ArrayObject
{
public function __serialize(): array
{
return $this->getArrayCopy();
}
public function __unserialize(array $data): void
{
$this->__construct($data);
}
}
$collection = new Collection([
'name' => 'Hello World',
'path' => [
'of' => [
'the' => 'Road',
'number' => 20,
'text_a' => ' Bad Boy!',
'text_b' => 'Good Boy! ',
],
],
]);
$s1 = serialize($collection);
var_dump($s1);
$s2 = unserialize($s1);
var_dump($s2);
Expected result:
----------------
string(195) "O:16:"Razy\CCollection":2:{s:4:"name";s:11:"Hello
World";s:4:"path";a:1:{s:2:"of";a:4:{s:3:"the";s:4:"Road";s:6:"number";i:20;s:6:"text_a";s:12:"
Bad Boy!";s:6:"text_b";s:12:"Good Boy! ";}}}"
object(Razy\CCollection)#66 (1) { ["storage":"ArrayObject":private]=>
array(2) { ["name"]=> string(11) "Hello World" ["path"]=>
array(1) {
["of"]=> array(4) { ["the"]=> string(4) "Road"
["number"]=> int(20)
["text_a"]=> string(12) " Bad Boy!" ["text_b"]=> string(12)
"Good Boy! "
} } } }
Actual result:
--------------
Warning: Declaration of Collection::__unserialize(array $data): void
should be compatible with ArrayObject::__unserialize($serialized) in
...
string(195) "O:16:"Razy\CCollection":2:{s:4:"name";s:11:"Hello
World";s:4:"path";a:1:{s:2:"of";a:4:{s:3:"the";s:4:"Road";s:6:"number";i:20;s:6:"text_a";s:12:"
Bad Boy!";s:6:"text_b";s:12:"Good Boy! ";}}}"
object(Razy\CCollection)#66 (1) { ["storage":"ArrayObject":private]=>
array(2) { ["name"]=> string(11) "Hello World" ["path"]=>
array(1) {
["of"]=> array(4) { ["the"]=> string(4) "Road"
["number"]=> int(20)
["text_a"]=> string(12) " Bad Boy!" ["text_b"]=> string(12)
"Good Boy! "
} } } }
--
Edit bug report at https://bugs.php.net/bug.php?id=80788&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80788&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80788&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80788&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80788&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80788&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80788&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80788&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80788&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80788&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80788&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80788&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80788&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80788&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80788&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80788&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80788&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80788&r=mysqlcfg