Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

php at werner dash ott dot de
18 years ago
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
?>

<< Back to user notes page

To Top