在PHP中,我考虑做这样的事情:不好的做法?它是什么“称为”
function foo(){
echo 'bar';
}
$fn = 'foo';
$fn();
它的工作原理,但它认为不好的做法?
我有一个多维数组,每个都有相应的功能。我想存储该函数名称,并在遍历数组时调用每个元素的相应函数。
类似:
function render_el1(){ echo 'et';}
function render_el2(){ echo 'to';}
$elements = array(
'el_1' => array(
'name' => 'Element One'
, 'func' => 'render_el1'
)
, 'el_2' => array(
'name' => 'Element Two'
, 'func' => 'render_el2'
)
);
foreach($elements as $element => $options){
$fn = $options['func'];
echo '<h1>'.$options['name'].'</h1>';
if (function_exists($fn)) {
$fn();
}
}
这种方法的任何评论是非常欢迎的,我也想知道这个方法被称为在编程方面。
我很想把它叫做reflectionesque邪恶,但我不知道这是答案材料:) – 2011-03-10 12:48:10