3

我有一个带有一些“过滤器”的数据库;即方法调用,并希望能够根据用户输入的内容动态调用它们。

Code Igniter 似乎不喜欢这样,有什么想法吗?它输出: 在 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
  }
4

3 回答 3

8

foreach 循环应该是这样的:

$test = $row->filterCountFunction;
$this->{$test}();
于 2012-07-09T13:02:44.417 回答
0

您可以使用call_user_func来调用带参数的动态函数

$result['emailCount'] = call_user_func('filter_function_name',$parameters);
于 2012-07-09T12:59:59.577 回答
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;
       }
于 2012-07-09T12:58:48.613 回答