我有一个带有一些“过滤器”的数据库;即方法调用,并希望能够根据用户输入的内容动态调用它们。
代码点火器似乎不喜欢这样,有什么想法吗? 它输出: 在GetEmailCountfilter_male_count()中:
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined property: Targeted_Email::$filter_male_count()</p>
<p>Filename: core/Model.php</p>
<p>Line Number: 51</p>
{"emailCount":null}
代码:
public function getEmailCount($filter){
echo 'In GetEmailCount';
$result = array();
$query = $this->db->get('base_targeted_email_filters');
foreach ($query->result() as $row){
if($filter == $row->name){
$test = $row->filterCountFunction . "()";
echo $test;
$result['emailCount'] = $this->{$test};
}
return $result
}
答案 0 :(得分:8)
foreach循环应该是这样的:
$test = $row->filterCountFunction;
$this->{$test}();
答案 1 :(得分:0)
以下代码可以使用。
public function getEmailCount($filter){
echo 'In GetEmailCount';
$result = array();
$query = $this->db->get('base_targeted_email_filters');
foreach ($query->result() as $row){
if($filter == $row->name){
$test = $row->filterCountFunction . "()";
echo $test;
$result['emailCount'] = $this->$test;
}
答案 2 :(得分:0)
您可以使用call_user_func通过参数
调用动态函数$result['emailCount'] = call_user_func('filter_function_name',$parameters);