php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80788 __unserialize type hinting conflict with ArrayObject::unserialize
Submitted: 2021-02-23 06:53 UTC Modified: 2021-02-23 08:44 UTC
From: hello at rayfung dot hk Assigned: nikic (profile)
Status: Closed Package: SPL related
PHP Version: 7.4.15 OS: MacOS Apple Silicon
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: hello at rayfung dot hk
New email:
PHP Version: OS:

 

 [2021-02-23 06:53 UTC] 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! " } } } }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-23 08:44 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2021-02-23 08:44 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Dec 05 18:03:37 2021 UTC