我尝试在Code Igniter控制器中定义数组$data
,如下所示。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* welcome controller
*/
$data = array();
public function index()
{
$this->load->view('welcome_message', $data);
}
}
?>
但是,此错误会浮出水面。
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in
/home/users/davidfaux/testApp/application/controllers/welcome.php on line 8
将$data
移至index()
函数可修复错误。为什么呢?
答案 0 :(得分:3)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* welcome controller
*/
**private** $data = array();
public function index()
{
$this->load->view('welcome_message', $this->data);
}
}
?>
答案 1 :(得分:0)
您也可以使用var关键字来实现
class Welcome extends CI_Controller {
/**
* welcome controller
*/
var $data = array();
public function index()
{
$this->load->view('welcome_message', $this->data);
}
}
?>