我正在尝试从同一个类中的函数访问类中声明的数组.我尝试了几种不同的方法来尝试使其工作,但我对PHP相对较新.这是我的代码片段
class Site extends CI_Controller {
var $dates = array(
"Task" => NULL,
"Date1" => NULL,
"Date2" => NULL,
"TimeDiff" => NULL
);
function index()
{
if($this->$dates['Date1'] != NULL && $this->$dates['Date2'] != NULL)
{
$this->$dates['TimeDiff'] = $this->$dates['Date2']->getTimestamp() - $this->$dates['Date1']->getTimestamp();
}
$this->load->view('usability_test', $this->$dates);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用global关键字
global $dates;
Run Code Online (Sandbox Code Playgroud)
无论如何我仍然会收到"未定义变量"错误.谢谢!
你想要$this->dates['Date1']而不是$this->$dates['Date1'].请注意$之前没有的dates.
作为旁注,请确保CI_Controller通过定义以下内容正确扩展__construct():
class Site extends CI_Controller {
// class properties, etc.
function __construct(){
parent::__construct();
}
// class methods, etc.
}
Run Code Online (Sandbox Code Playgroud)
另外需要注意的var是,自PHP5起就弃用了.您将要为使用public,private或者protected根据您的需要(编辑:假设,当然,前提是你正在使用PHP5).
| 归档时间: |
|
| 查看次数: |
5483 次 |
| 最近记录: |