Home > Backend Development > PHP Tutorial > 在ci框架中定义了一个变量,并且已经初始为一个值,我想在其他方法中调用这个变量的值,如何做

在ci框架中定义了一个变量,并且已经初始为一个值,我想在其他方法中调用这个变量的值,如何做

WBOY
Release: 2016-06-06 20:41:38
Original
1037 people have browsed it

大致情况 如此
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()生成的对象,在后面的方法中可以直接访问

<code>$ci = & get_instance(); 
//需要全局调用的变量
$variable = array('var1'=>"value1",'var2'=>"value2");
//在控制器和视图构造函数中
//据说相当于在构造函数里调用了extract
//参考 http://codeigniter.org.cn/user_guide/libraries/loader.html
$ci->load->vars($variable);
</code>
Copy after login

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

global $resultMap;

建议:放到配置中用 config_item() 获取!

<code>config_item('name')
</code>
Copy after login

& get_instance()config_item() 都可以,
感觉 config_item 会更好些

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template