In which case use eval()
Whats so unsecure about the following code?
Code: Select all
class Contacts{
function __construct($name, $title, $email)
{
$this->name = $name;
$this->title = $title;
$this->email = $email;
}
private $name;
private $title;
private $email;
}
$arg = array('"Alex"', '"Coordinator"', '"nuweb1@hotmail.com"');
$cls = 'Contacts';
eval('$obj = new '.$cls.'('.(implode(', ', $arg)).');');
print_r( $obj );
You could call preg_match() on $cls and/or use some convention which only your ORM objects are likely to follow (prevent incorrect object instantiation) and follow the same process for $arg...
All in all relatively easy...fairly straight forward and does what you are asking...I think...
I'd go with eval() as it's your only option...or at least guaranteed to work on any version of PHP...
Or you can go with arrays as parameter lists and use the dreaded
get_func_args() type functions and then inside the function call
extract()?
Cheers
