|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-11-12 20:21 UTC] machine dot check dot exception at gmail dot com
Description:
------------
Doing tests I found a different behaviour between versions of php.
The code in the test script returns in php 5.2.7, 5.3.0, 5.3.9, this:
O:8:"stdClass":1:{s:1:"a";s:4:"tmp1";}
O:8:"stdClass":1:{s:1:"a";s:4:"tmp2";}
In php 5.4.0RC8, 5.4.3 and 5.5.5 this:
O:8:"stdClass":1:{s:1:"a";s:4:"tmp1";}
r:3;
Test script:
---------------
class test implements Serializable {
public $a;
public function __construct( $_val ){
$this->a = $_val;
}
public function serialize() {
$tmp = (object) array();
$tmp->a = $this->a;
echo serialize( $tmp ) . "\n";
return serialize( $tmp );
}
}
$n = array();
$n[0] = new test('tmp1');
$n[1] = new test('tmp2');
print_r( unserialize( serialize( $n ) ) );
Expected result:
----------------
O:8:"stdClass":1:{s:1:"a";s:4:"tmp1";}
O:8:"stdClass":1:{s:1:"a";s:4:"tmp2";}
Actual result:
--------------
O:8:"stdClass":1:{s:1:"a";s:4:"tmp1";}
r:3;
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 07:00:01 2025 UTC |
Yup, zval ids/addresses are getting reused. It looks like $tmp is getting collected after the test serialize() method. If I *prevent* the collection by keeping a reference to the inner $tmp in a global array, then I get the expected output: $keep_ref = array(); class test implements Serializable { public $a; public function __construct( $_val ){ $this->a = $_val; } public function serialize() { $tmp = (object) array(); $tmp->a = $this->a; array_push($keep_ref, $tmp); // keep hold of a reference echo serialize( $tmp ) . "\n"; return serialize( $tmp ); } } I tested with the 9 value array example here: http://3v4l.org/DJg0j Can anybody confirm? If zval id/address is used as the visited object key, and these are getting reused/replaced by garbage collection while walking the object graph, then I'm not sure how we can implement references across the entire object graph. Is there a way to temporarily disable collecting reference-counted objects? I see there is gc_disable/gc_enable that appears to only affect "circular" reference collector.