php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45299 serialize / unserialize $GLOBALS breaks global objects
Submitted: 2008-06-18 12:03 UTC Modified: 2008-07-04 08:43 UTC
From: taco at procurios dot nl Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.6 OS: linux
Private report: No CVE-ID: None
 [2008-06-18 12:03 UTC] taco at procurios dot nl
Description:
------------
When serializing and later on deserializing the $GLOBALS array, global objects aren't recreated.

Reproduce code:
---------------
class Foo
{
	public $foo;
	public function __construct($foo)
	{
		$this->foo = $foo;
	}
}
$Foo = new Foo(34);
var_dump($GLOBALS['Foo']);

$s = serialize($GLOBALS);
$GLOBALS = unserialize($s);

var_dump($GLOBALS['Foo']);

Expected result:
----------------
object(Foo)#1 (1) {
  ["foo"]=>
  int(34)
}
object(Foo)#1 (1) {
  ["foo"]=>
  int(34)
}

Actual result:
--------------
object(Foo)#1 (1) {
  ["foo"]=>
  int(34)
}
int(1)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-03 11:53 UTC] jani@php.net
Please tell us WHY do you want to serialize special super global arrays like $GLOBALS in the first place?! That's propably the dummiest idea ever..
 [2008-07-03 11:55 UTC] taco at procurios dot nl
I don't ;)

I encountered this 'bug' while using PHPUnit, see: http://www.phpunit.de/browser/phpunit/trunk/PHPUnit/Framework/TestCase.php#L370
 [2008-07-04 08:16 UTC] jani@php.net
Complain to PHPUnit people then.
 [2008-07-04 08:43 UTC] taco at procurios dot nl
Sebastian Bergmann should not serialize $GLOBALS or I should turn off this feature in my PHPUnit test code (which I did).

I can agree with that, but that does not change the fact that it is possible to serialize and unserialize the $GLOBALS array and that it breaks objects. PHP should either not allow the $GLOBALS array to be serialized, as it does with PHP built-in objects, or should unserialize the array correctly.

This bug might be minor, but still it is a bug.

While trying to figure out a workaround for the PHPUnit code I found out that a global variable that is 'broken' by this behaviour cannot be restored, add the following lines to the original reproduce code:

$Foo = new Foo(12);
var_dump($GLOBALS['Foo']);

global $Foo;
var_dump($GLOBALS['Foo']);

The value in the $GLOBALS array doesn't change anymore, sounds like pretty buggy to me. (Bad practice or not)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Nov 21 18:00:01 2025 UTC