相关疑难解决方法(0)

什么时候是PHP的eval邪恶?

在我用PHP开发的这些年里,我总是听说使用eval()是邪恶的.

考虑以下代码,使用第二个(更优雅)选项是否有意义?如果没有,为什么?

// $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')";

// possibility one
$type_1 = preg_replace('#^enum\s*\(\s*\'|\'\s*\)\s*$#', '', $type);
$result = preg_split('#\'\s*,\s*\'#', $type_1);

// possibility two
eval('$result = '.preg_replace('#^enum#','array', $type).';');
Run Code Online (Sandbox Code Playgroud)

php eval

82
推荐指数
9
解决办法
4万
查看次数

是否可以动态地向/扩展类添加代码?

我想为我的代码编写一种"插件/模块"系统,如果我可以在定义后将其"添加"到类中,它会更容易.

例如,像这样:

class foo {
  public function a() {
     return 'b';
  }
}
Run Code Online (Sandbox Code Playgroud)

这是班级.现在我想在定义后添加另一个函数/变量/ const.

我意识到这可能是不可能的,但我需要确认.

php

4
推荐指数
1
解决办法
2657
查看次数

标签 统计

php ×2

eval ×1