note 74192 added to ref.simplexml

From: Date: Thu, 29 Mar 2007 09:13:13 +0000
Subject: note 74192 added to ref.simplexml
Groups: php.notes 
Request: Send a blank email to php-notes+get-124912@lists.php.net to get a copy of this message
Making SimpleXMLElement objects session save. Besides the effect of not surviving sessions, the SimpleXMLElement object may even crash the session_start() function when trying to re-enter the session! To come up with a solution for this, I used a pattern as follows. The core idea is to transform the SimpleXMLElement between session calls to and from a string representation which of course is session save. <?php // // session save handling of SimpleXMLElement objects // (applies to/ tested with PHP 5.1.5 and PHP 5.2.1) // The myClass pattern allows for conveniently accessing // XML structures while being session save // class myClass { private $o_XMLconfig = null; private $s_XMLconfig = ''; public function __construct($args_configfile) { $this->o_XMLconfig = simplexml_load_file($args_configfile); $this->s_XMLconfig = $this->o_XMLconfig->asXML(); } // __construct() public function __destruct() { $this->s_XMLconfig = $this->o_XMLconfig->asXML(); unset($this->o_XMLconfig); // this object would otherwise crash // the subsequent call of // session_start()! } // __destruct() public function __wakeup() { $this->o_XMLconfig = simplexml_load_string($this->s_XMLconfig); } // __wakeup() } // class myClass ?> ---- Server IP: 217.160.72.57 Probable Submitter: 217.24.12.178 ---- Manual Page -- http://www.php.net/manual/en/ref.simplexml.php Edit -- https://master.php.net/note/edit/74192 Del: integrated -- https://master.php.net/note/delete/74192/integrated Del: useless -- https://master.php.net/note/delete/74192/useless Del: bad code -- https://master.php.net/note/delete/74192/bad+code Del: spam -- https://master.php.net/note/delete/74192/spam Del: non-english -- https://master.php.net/note/delete/74192/non-english Del: in docs -- https://master.php.net/note/delete/74192/in+docs Del: other reasons-- https://master.php.net/note/delete/74192 Reject -- https://master.php.net/note/reject/74192 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124912) next »