In all the years I have been developing in PHP, I have always heard that using eval()
is evil.
Considering the following code, wouldn't it make sense to use the second (and more elegant) option? If not, why?
// $type is the result of an SQL statement, e.g.
// SHOW COLUMNS FROM a_table LIKE 'a_column';
// hence you can be pretty sure about the consistency
// of your string.
$type = "enum('a','b','c')";
// option one
$type_1 = preg_replace('#^enums*(s*'|'s*)s*$#', '', $type);
$result = preg_split('#'s*,s*'#', $type_1);
// option two
eval('$result = '.preg_replace('#^enum#','array', $type).';');
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…