我在代码点火器应用程序中有这个控制器。值在构造函数中初始化。
class Cat extends CI_Controller {
private $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $data);
}
}
视图"views/myCatPage.php"如下所示。这很简单。
<?= $sound ?>
为什么 PHP 会注意到此错误?
Message: Undefined variable: sound
我以为我把这个变量作为我发送到视图中的数组($data
)中的一个键发送。我试过了
$this->load->view('myCatPage', $this->data);
但奇怪的是,这也失败了。
class Cat extends CI_Controller {
var $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $this->data);
}
}