#45816 [Com]: serialize() modifies objects

[email protected] ("gmtfn at yahoo dot com")
Newsgroups php.bugs
Message-ID <[email protected]>
 ID:               45816
 Comment by:       gmtfn at yahoo dot com
 Reported By:      gmtfn at yahoo dot com
 Status:           Feedback
 Bug Type:         Session related
 Operating System: Linux
 PHP Version:      5.2.6
 Assigned To:      fb-req-jani
 New Comment:

register_globals is on on Linux servers and off on Vista. But whatever
its value is, getting a string representation of an object, which
serialization is, shouldn't change the object. It's completely
counterintuitive, and I can't imagine a valid reason for ruining a still
perfectly usable object. It's analogous to changing a variable's type
just because it's been typecasted to something else.


Previous Comments:
------------------------------------------------------------------------

[2008-08-14 17:42:38] [email protected]

Check your phpinfo() output that register_globals is Off.

------------------------------------------------------------------------

[2008-08-14 07:48:52] gmtfn at yahoo dot com

line 25, not 26

------------------------------------------------------------------------

[2008-08-14 07:37:54] gmtfn at yahoo dot com

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 this bug report at http://bugs.php.net/?id=45816&edit=1
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.