【发布时间】:2012-06-09 18:01:17
【问题描述】:
我想在一个 php 类中声明一个变量,它可以在它的所有函数中使用。我正在使用 codeigniter 框架,并且必须在每个函数中传递 base_url 等数据。我还需要一个变量中的所有这些数据。例如$data['base_url']=base_url()、$data['title']="My_Site_title" 等,然后只发送一个变量 $data(这在 codeigniter 中很常见)。
根据this 的问题,我将代码修改为:
class Search extends CI_Controller {
function Search() {
parent::__construct();
$this->load->model('student_model');
$this->load->helper('form');
$this->load->helper('url');
$this->output->enable_profiler(TRUE);
$this->data['base_url']=base_url(); //gives an error here
}
function index() {
$this->load->view('search_view', $this->data);
}
}
但它仍然在标记的地方给出错误,说Undefined variable: data和Fatal error: Cannot access empty property in ..\search.php on line 16。
【问题讨论】:
-
不能在没有实例的情况下使用 $data
标签: php class codeigniter scope codeigniter-2