
Originally Posted by
kyberfabrikken
Yep. The point exactly. The one I posted will allow you to create multiple named dataspaces throughout the application, which will persist between sessions.
I haven't followed where you have taken the DataSpace class. I was just looking around the web for ideas and combined some things I found into the somewhat kooky code below.
This DataSpace allows for paths into the dataspace array. The problem with this code is that uses eval() so there might be some security issues, but I think because the paths all come from the programmer it is ok.
PHP Code:
class DataSpace {
var $data = array();
var $source = '$this->data';
var $delimiter = '.';
function set($path, $value) {
eval($this->source . '[\'' . str_replace($this->delimiter, "']['", $path) . '\'] = $value;');
}
function get($path) {
$str = $this->source . '[\'' . str_replace($this->delimiter, "']['", $path) . '\']';
eval("\$value = (isset($str) ? $str : null);");
return $value;
}
}
I took the code from the Session class above but changed it so it doesn't start the session until a value is actually accessed. This is so you can still create a session object early and not have sent headers already which I have found to be a problem with things like upload pages.
PHP Code:
class Session extends DataSpace {
function Session() {
$this->source = '$_SESSION';
}
function get($name) {
if (session_id() == "") {
session_start();
}
return parent::get($name);
}
function set($name, $value) {
if (session_id() == "") {
session_start();
}
parent::set($name, $value);
}
}
You can use it with the '.' delimeter Java/WACT style (or set your own delimeter if you want):
PHP Code:
$session =& new Session();
$session->set('products.SKU123.color', 'blue');
$session->set('products.SKU123.price', 9.95);
$sku = $session->get('products.SKU123');
// returns array('color'=>'blue', 'price'=>9.95)
Bookmarks