note 74192 modified in ref.simplexml by danbrown

From: Date: Mon, 18 May 2009 01:22:05 +0000
Subject: note 74192 modified in ref.simplexml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155525@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 ?> --was-- 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 ?> http://php.net/manual/en/ref.simplexml.php

« previous php.notes (#155525) next »