#45816 [NEW]: serialize() modifies objects

[email protected] ("gmtfn at yahoo dot com")
Newsgroups php.bugs
Message-ID <[email protected]>
From:             gmtfn at yahoo dot com
Operating system: Linux
PHP version:      5.2.6
PHP Bug Type:     Session related
Bug description:  serialize() modifies objects

Description:
------------
Calling serialize() on an object whose name is the same as the key under
which it's saved in $_SESSION makes the object unusable (its methods can't
be called.

If I store store a serialized version of the object under a key that's
different from the name of the variable that points to the object, then
everything works fine. If this is by design and must remain this way, I
want to tell you that this is VERY counterintuitive and must be prominently
documented.

I can reproduce this on a Linux server (with PHP 5.2.5 and 5.2.6) but not
on Windows Vista.

Reproduce code:
---------------
<?php
	session_start();
	ob_start();

	class GoodObject {
		private $count = 0;
		public function doSmth()
		{
			echo "$this->count <br>";
			$this->count++;
		}
	}

	if(!isset($_SESSION['myObj'])) {
		$myObj = new GoodObject();
	}
	else {
		$s = $_SESSION['myObj'];
		$myObj = unserialize($s);
	}
	
	$myObj->doSmth();
	$s = serialize($myObj);
	$_SESSION['myObj'] = $s;
	$myObj->doSmth(); // this fails on the second page load
?>

Expected result:
----------------
Converting an object to a string should not change the object, and the
last statement in this script should output the value of $count.

Actual result:
--------------
On the second load of the page, I get this:

Fatal error: Call to a member function doSmth() on a non-object in [path
to the file] on line 26

-- 
Edit bug report at http://bugs.php.net/?id=45816&edit=1
-- 
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=45816&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=45816&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=45816&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=45816&r=fixedcvs
Fixed in release:             http://bugs.php.net/fix.php?id=45816&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=45816&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=45816&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=45816&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=45816&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=45816&r=notwrong
Not enough info:              http://bugs.php.net/fix.php?id=45816&r=notenoughinfo
Submitted twice:              http://bugs.php.net/fix.php?id=45816&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=45816&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=45816&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=45816&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=45816&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=45816&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=45816&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=45816&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=45816&r=mysqlcfg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.