Bug #80788 [Opn->Csd]: __unserialize type hinting conflict with ArrayObject::unserialize

From: Date: Tue, 23 Feb 2021 08:44:46 +0000
Subject: Bug #80788 [Opn->Csd]: __unserialize type hinting conflict with ArrayObject::unserialize
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232336@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80788&edit=1 ID: 80788 Updated by: nikic@php.net Reported by: hello at rayfung dot hk Summary: __unserialize type hinting conflict with ArrayObject::unserialize -Status: Open +Status: Closed Type: Bug Package: SPL related Operating System: MacOS Apple Silicon PHP Version: 7.4.15 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: Extension-provided functions/methods have only been annotated with type information in PHP 8.0. For that reason, using types on arguments of overridden internal methods is usually not possible in earlier PHP versions. Previous Comments: ------------------------------------------------------------------------ [2021-02-23 06:53:31] hello at rayfung dot hk 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 this bug report at https://bugs.php.net/bug.php?id=80788&edit=1

« previous php.bugs (#232336) next »