Hi all!
I need some of my objects to be able to load themselves from session arrays. For some particular reasons, I need all functionality of the class to be native to the class, so no external loader functions.
The particular problem is loading all object properties from (session) array. If done with an external function, I might simply unserialize entire object, but that's not what I need. I need object method to load up its own properties from array.
Is there a fast way, or way native to PHP to do this? I thought extract() might help, but I don't know how to tell it to use class namespace as symbol table.
For instance, I thought to do it like this:
class TClass {
protected var $a;
protected var $b;
protected var $c;
protected function loadfromsession() {
foreach ($_SESSION['data'] as $var => $val) {
if (ctype_digit($val)) eval "\$this->$var= $val;";
else eval "\$this->$var= '$val';";
}
}
// ...
}