当不在Codeigniter的对象上下文中时使用$this


Using $this when not in object context in Codeigniter

我知道这个问题已经有答案了,但我不明白,所以又问了一次所以请帮忙

这个错误到底意味着什么?

致命错误:在中不在对象上下文中时使用$thisC: ''wamp''www''provision''application''controllers''customer''provision.php在线27

public function index(){
  $users = $this->data['tenant_users'] = $this->customer_user_m->tenant_users();
  $domain = $users[0]['domain'];
  $site = $users[0]['site_key'];
  $tenant_id = $users[0]['tenant_id'];
  $site = $this->session->userdata('site');
  $user_table = $this->session->userdata('user_table');
  function getOTAURLExt($ext){
    var_dump($this);
  }
  function getOTAURLSite(){
    echo "Site executed";
  }
  $this->db->select('*');
  $this->db->where('id', $tenant_id);
  $this->db->from('tenant');
  $query = $this->db->get();
  $result = $query->result_array();
  if(empty($this->input->post('md'))){
    $URL = getOTAURLSite($site);
  }else{
    $username = $result[0]['username'];
    $table_user = $username . "_users";

    $this->db->select('*');
    $this->db->where($table_user . '.site_key', $site);
    $this->db->join('mlcsites', 'mlcsites.site_key =' . $table_user . '.site_key');
    $this->db->from($table_user);
    $query_table = $this->db->get();
    $information = $query_table->result_array();

    $ext = $information[0]['ext'];

    $count = count($information);
    $found = false;
    for($i = 0; $i < $count; $i++){
      $domain = $information[$i]['domain'];
      $ext = $information[$i]['ext'];
      $hash = do_hash($ext . "@" . $domain, 'md5');
      if($hash == $this->input->post('md')){
        $found = true;
        break;
      }
    }
    if($found == true){
      $URL = getOTAURLExt($ext);
    }
  }
  if(empty($URL)){
  }
  $this->data['subview'] = 'provisioning/index';
  $this->load->view('_layout_main', $this->data);
}

对此可能的解决方案是什么?

根据您提供的代码,"第27行"上似乎没有任何内容,因此IMHO似乎是由"第10行"引起的

function getOTAURLExt($ext){
    var_dump($this); // <<< This seems to be causing error.
}

现在,如果你需要函数中的变量$this,你可以选择

  1. 将其作为参数传递(如果您只需要从对象中读取值)
  2. 通过引用传递(如果需要更新对象。)
  3. 将其声明为全局

然后在函数中使用它。