Dynamic argument passing question
Posted: Sun Apr 15, 2007 6:30 pm
Working with PHP 4, I'm trying to pass arguments dynamically. Here's my code.
As I've commented above if $class is an array shift the first element off as the name. Then pass the rest of the array as arguments into the classes constructor as $arg0, $arg1, $arg2 and so forth.
Can this be achieved?
Code: Select all
function load($class)
{
//If $class is an array assume the first index is the class name.
if(is_array($class))
{
$class_name = array_shift($class);
}
$class_path = str_replace('_', '/', $class_name);
if(file_exists($_SERVER['DOCUMENT_ROOT'].'/'.$class_path.'.php'))
{
include_once($_SERVER['DOCUMENT_ROOT'].'/'.$class_path.'.php')
{
//If $class was an array pass the arguments here as
// $arg1, $arg2, $arg3 and so forth.
return new $class_name;
}
}
}Can this be achieved?