在ci架構中定義了一個變數,並且已經初始為一個值,我想在其他方法中調用這個變數的值,如何做

來源:互聯網
上載者:User
大致情況 如此
public function x()
{
$data['resultMap']=$resultMap;
//print_r($resultMap);
$this->load->view('data_list',$data);
}
public function y()
{//想把第一個函數中的$resultMap的值賦值給$data;
$data=array();

}

回複內容:

大致情況 如此
public function x()
{
$data['resultMap']=$resultMap;
//print_r($resultMap);
$this->load->view('data_list',$data);
}
public function y()
{//想把第一個函數中的$resultMap的值賦值給$data;
$data=array();

}

把這個變數賦值給&get_instance()產生的對象,在後面的方法中可以直接存取

$ci = & get_instance(); //需要全域調用的變數$variable = array('var1'=>"value1",'var2'=>"value2");//在控制器和視圖建構函式中//據說相當於在建構函式裡調用了extract//參考 http://codeigniter.org.cn/user_guide/libraries/loader.html$ci->load->vars($variable);

參考:http://baiyuxiong.iteye.com/blog/797446

global $resultMap;

建議:放到配置中用 config_item() 擷取!

config_item('name')

& get_instance()config_item() 都可以,
感覺 config_item 會更好些

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.