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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 03:01:29 2024 UTC