|
Posted by daniel on 01/17/05 07:59
Hi list, I am needing some advise on the best way of importing classes and
persistance, I am goingto supply some code, and would like to know if sessions or share memory is
the best way for it.It currently serializes and unserializes the class from a session , i
parse the directory and classusing pear naming standards, i can also send args to it. Let me know if
there is a better way ofdoing this, as from the talk serializing is also slow. I wonder if this
bit of code could ever becomea php extension ?? That would rock.
import 'PACKAGE_Classname'; or even import PACKAGE_Classname heh :)
becomes require_once('PACKAGE/Classname.php');
new PACKAGE_Classname(); //persist here dont reinclude per page.
function import($class_name,$serialize = null,$parse_dir = true,$args = null)
{
$parse_dir ? $filename = preg_replace("/_/","/",$class_name) : $filename =
$class_name;
if ($serialize) {
require_once($filename.".php");
if (!isset($_SESSION[''.$class_name.''])) {
if (!$args) {
$class = new $class_name;
} else {
$num_args = func_num_args();
$params = func_get_args();
$class= $this->_format_args($class_name,$num_args,$params);
}
$_SESSION[''.$class_name.''] = serialize($class);
}
return unserialize($_SESSION[''.$class_name.'']);
} else {
require_once($filename.".php");
if (!$args) {
return new $class_name;
} else {
$num_args = func_num_args();
$params = func_get_args();
return $this->_format_args($class_name,$num_args,$params);
}
}
}
function _format_args($class_name,$num_args,$params)
{
$code = "return new {$class_name}(";
if ($num_args >= 4) {
$args = array();
for($i=3;$i<$num_args;$i++) {
$args[] = '$params[' . $i . ']';
}
$code .= implode(',',$args);
}
$code .= ');';
return eval($code);
}
Navigation:
[Reply to this message]
|