note 91757 added to language.oop.serialization

From: Date: Wed, 24 Jun 2009 18:29:19 +0000
Subject: note 91757 added to language.oop.serialization
Groups: php.notes 
Request: Send a blank email to php-notes+get-157436@lists.php.net to get a copy of this message
In both PHP4 and PHP5, references are lost when serializing/unserializing (contrary to what is claimed by kendlj at NOSPAM dot web dot de, below). This is kind of understandable, but something to watch out for. Example code: <?php class Test { var $a = array(); function Test(&$Value) { $this->a['byval'] = $Value; $this->a['byref'] =& $Value; } } $MyVar = "original value"; $obj = new Test($MyVar); print("After original creation:\n"); print_r($obj); $MyVar = "second value"; print("\nAfter changing variable value:\n"); print_r($obj); $obj = unserialize(serialize($obj)); print("\nAfter serialize/unserialize:\n"); print_r($obj); $MyVar = "third value"; print("\nAfter changing variable value again:\n"); print_r($obj); ?> Output: ====== After original creation: object(Test)#1 (1) { ["a"]=> array(2) { ["byval"]=> string(14) "original value" ["byref"]=> &string(14) "original value" } } After changing variable value: object(Test)#1 (1) { ["a"]=> array(2) { ["byval"]=> string(14) "original value" ["byref"]=> &string(12) "second value" } } After serialize/unserialize: object(Test)#2 (1) { ["a"]=> array(2) { ["byval"]=> string(14) "original value" ["byref"]=> string(12) "second value" } } After changing variable value again: object(Test)#2 (1) { ["a"]=> array(2) { ["byval"]=> string(14) "original value" ["byref"]=> string(12) "second value" } } ---- Server IP: 69.147.83.197 Probable Submitter: 212.225.120.29 ---- Manual Page -- http://www.php.net/manual/en/language.oop.serialization.php Edit -- https://master.php.net/note/edit/91757 Del: integrated -- https://master.php.net/note/delete/91757/integrated Del: useless -- https://master.php.net/note/delete/91757/useless Del: bad code -- https://master.php.net/note/delete/91757/bad+code Del: spam -- https://master.php.net/note/delete/91757/spam Del: non-english -- https://master.php.net/note/delete/91757/non-english Del: in docs -- https://master.php.net/note/delete/91757/in+docs Del: other reasons-- https://master.php.net/note/delete/91757 Reject -- https://master.php.net/note/reject/91757 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157436) next »