gpt4 book ai didi

codeigniter - 构造函数中的代码点火器 2.0 变量

转载 作者:行者123 更新时间:2023-12-03 10:07:39 24 4
gpt4 key购买 nike

我有一个小的 codeigniter Controller 。下面是代码

class Example extends CI_Controller {
/*
* Constructor function
*/

function __construct() {
parent::__construct();
$data['extraScripts'] = 'test'; //Use to add extra scripts in head


}

function function1() {
$this->load->view('v1',$data);

}

function function2() {
$data['extraScripts'] = 'extraScript Veriable override here';
$this->load->view('v2',$data);
}

我想要的是定义一个真实的 $data['extraScripts']在 Controller 的构造函数中,并希望默认情况下在该 Controller 的每个方法中都是可验证的。我的意思是在函数 f1 中我没有创建 extraScripts 变量,但它的 View 应该从构造函数(或任何其他方法)中获取值,并且不应该给我 undefine 变量错误。在第二个函数 f2 中,我覆盖了 extraScript 变量,因此它的 View 应该显示覆盖的文本。那可能吗。

最佳答案

使 $data 成为一个属性(基本的 OOP)。

例如;

class Example extends CI_Controller {
/*
* Constructor function
*/

public $data = array();

function __construct() {
parent::__construct();
$this->data['extraScripts'] = 'test'; //Use to add extra scripts in head


}

function function1() {
$this->load->view('v1',$this->data);

}

function function2() {
$data['extraScripts'] = 'extraScript Veriable override here';
$this->load->view('v2',$this->data);
}
}

关于codeigniter - 构造函数中的代码点火器 2.0 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8985359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com