有谁知道如何访问已由另一个函数更新的公共控制器变量的值?
代码示例
调节器
代码示例
调节器
class MyController extends CI_Controller { public $variable = array(); function __construct() { parent::__construct(); } function index(){ $this->variable['name'] = "Sam"; $this->variable['age'] = 19; } function another_function(){ print_r($this->variable); }
}
当我调用another_function()时,我得到一个空数组..可能是什么问题?
任何帮助将是apreciated ..
你必须使用构造函数,而不是index().
原文链接:https://www.f2er.com/php/134900.htmlclass MyController extends CI_Controller { public $variable = array(); function __construct() { parent::__construct(); $this->variable['name'] = "Sam"; $this->variable['age'] = 19; } function index(){ } function another_function(){ print_r($this->variable); } }
如果要调用index(),则调用another_function(),尝试使用CI session类.
class MyController extends CI_Controller { public $variable = array(); function __construct() { parent::__construct(); $this->load->library('session'); if ($this->session->userdata('variable')) { $this->variable = $this->session->userdata('variable'); } } function index(){ $this->variable['name'] = "Sam"; $this->variable['age'] = 19; $this->session->set_userdata('variable',$this->variable); } function another_function(){ print_r($this->variable); } }