Code: Select all
function loadClass($file,$name,$opts=array()){
include $file;
return eval("new {$name}(".implode(',',$opts).")");
}This leads to 2 questions, how do I fix the errors? and is there a better way?
Moderator: General Moderators
Code: Select all
function loadClass($file,$name,$opts=array()){
include $file;
return eval("new {$name}(".implode(',',$opts).")");
}Code: Select all
$foo =& new $name;
call_user_func_array(array($foo, 'initialize'), $opts);
return $foo;Yay for static factory methods!feyd wrote:If the classes had an initialization method, not unlike the constructor's purpose, you could avoid eval() entirely usingCode: Select all
$foo =& new $name; call_user_func_array(array($foo, 'initialize'), $opts); return $foo;
Code: Select all
class Foo {
function Foo($arg1, $arg2, $arg3) {
//
}
function &newInstance($arg1, $arg2, $arg3) {
$instance =& new Foo($arg1, $arg2, $arg3);
return $instance;
}
}Code: Select all
$obj =& call_user_func_array(array("Foo", "newInstance"), $argList);