小编Ale*_*dar的帖子

Php(eval vs call_user_func vs变量函数......)

即使有关于这个问题的一些讨论,我想检查某些例子什么是最好的方法.
我没有使用现有的解决方案,而是创建了自己的持久层(就像许多人一样)所以我的方法在这里也有问题.

对于db中的每个表,我都有具有适当的getter和setter以及一些必需方法的模型类.我还只创建了一个处理所有类型的模型对象的通用DAO类.
因此,例如,为了保存任何模型对象,我实例化genericDAO类并调用我将模型对象作为属性传递的save方法.问题是在运行时,genericDAO类不知道它获取的whitch模型对象以及它中存在哪些方法(getter和setter),所以我需要调用强制模型类方法,该方法将属性列表检索为多个字符串数组.
例如,对于每个属性,都有数组(table_column_name,attribute_name,is_string).

当我调用save函数时,它看起来像这样:

public function save(&$VO) {  
$paramArray = $VO->getParamArray();//get array of attributes
$paramIdArray = $paramArray[0];  //first attribute is always id
/*create and execute getId() and store value into $void to check if it's save or update*/
eval('$voId = $VO->get'.ucfirst($paramIdArray[1]).'();');  
...
Run Code Online (Sandbox Code Playgroud)

目前我正在使用eval来执行这些方法,但众所周知,eval非常慢.
我正在考虑将其更改为call_user_func方法,
例如:

$voId = call_user_func(array($VO, 'get'.ucfirst($paramIdArray[1])));
Run Code Online (Sandbox Code Playgroud)

但也有其他解决方案.我可以使用这样的东西

$method = 'get'.ucfirst($paramIdArray[1]));
$voId = $VO->$method();
Run Code Online (Sandbox Code Playgroud) 要不然

$method = 'get'.ucfirst($paramIdArray[1]));
$voId = $VO->{$method}();
Run Code Online (Sandbox Code Playgroud)

什么是最好的方式?

php

5
推荐指数
1
解决办法
4496
查看次数

标签 统计

php ×1