php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76300 Serialize/Unserialize of extended protected member broken
Submitted: 2018-05-03 18:57 UTC Modified: 2018-05-03 19:48 UTC
From: rishad at kaluma dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 7.2.5 OS: Ubuntu 16.04
Private report: No CVE-ID: None
 [2018-05-03 18:57 UTC] rishad at kaluma dot com
Description:
------------
$php -v

```
PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr  5 2018 08:53:57) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.4-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans
```

I have a base class and derived class with same member name.
In the base class if member is protected it breaks the serialize/unserialize.
However, if the member is private functionality works as expected. 

This works on:
PHP 7.1.11-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Oct 27 2017 13:49:56) ( NTS )

Test script:
---------------
<?php
class Base {
	private $id;
	public function __construct($id)
	{
		$this->id = $id;
	}
}
class Derived extends Base {
	protected $id;
	public function __construct($id)
	{
		parent::__construct($id + 20);
		$this->id = $id;
	}
}
$a = new Derived(44);
$s = serialize($a);
$u = unserialize($s);
print_r($u);

Expected result:
----------------
Derived Object
(
    [id:protected] => 44
    [id:Base:private] => 64
)


Actual result:
--------------
Derived Object
(
    [id:protected] => 64
    [id:Base:private] => 
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-03 19:41 UTC] cedricleong at gmail dot com
This is not the serialize function break, rather you are overriding the base classes variable by assigning the same scope (protected) and same variable name ($id).
 [2018-05-03 19:48 UTC] nikic@php.net
-Status: Open +Status: Verified
 [2018-05-03 20:31 UTC] pmmaga@php.net
I have opened a PR for the fix: https://github.com/php/php-src/pull/3233
 [2018-06-17 22:53 UTC] stas@php.net
Automatic comment on behalf of mail@pmmaga.net
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2dca8671e4590917a52a4cd9cb57951b412e2f95
Log: Fix #76300 - Dont attempt to change visibility of a parent private
 [2018-06-17 22:53 UTC] stas@php.net
-Status: Verified +Status: Closed
 [2018-06-17 22:54 UTC] stas@php.net
Automatic comment on behalf of mail@pmmaga.net
Revision: http://git.php.net/?p=php-src.git;a=commit;h=13e9a471d79b5f845d087dd97c2f96d04493151e
Log: Fix #76300 - Dont attempt to change visibility of a parent private
 [2018-06-17 23:44 UTC] stas@php.net
Automatic comment on behalf of mail@pmmaga.net
Revision: http://git.php.net/?p=php-src.git;a=commit;h=13e9a471d79b5f845d087dd97c2f96d04493151e
Log: Fix #76300 - Dont attempt to change visibility of a parent private
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 08:01:33 2024 UTC