CodeIgniter PHP从同一类中的函数访问类中声明的变量
我试图从同一类中的函数访问类中声明的数组。我尝试了几种不同的方法来让它工作,但我对PHP比较陌生。这是我的代码片段CodeIgniter PHP从同一类中的函数访问类中声明的变量,php,codeigniter,Php,Codeigniter,我试图从同一类中的函数访问类中声明的数组。我尝试了几种不同的方法来让它工作,但我对PHP比较陌生。这是我的代码片段 class Site extends CI_Controller { var $dates = array( "Task" => NULL, "Date1" => NULL, "Date2" => NULL, "TimeDiff" => NULL ); function in
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);
}
我还尝试使用global关键字
global $dates;
不管怎样,我仍然会收到一个“未定义变量”错误。谢谢 您想要的是
$this->dates['Date1']
而不是$this->$dates['Date1']
。注意日期之前没有$
作为旁注,请确保通过如下方式定义\u构造()
来正确扩展CI\u控制器
:
class Site extends CI_Controller {
// class properties, etc.
function __construct(){
parent::__construct();
}
// class methods, etc.
}
另一件需要注意的事情是,var
从PHP5开始就被弃用了。根据您的需要,您可以使用public
、private
或protected
(编辑:当然,假设您使用的是PHP5)。您想要的是$this->dates['Date1']
而不是$this->$dates['Date1']
。注意日期之前没有$
作为旁注,请确保通过如下方式定义\u构造()
来正确扩展CI\u控制器
:
class Site extends CI_Controller {
// class properties, etc.
function __construct(){
parent::__construct();
}
// class methods, etc.
}
另一件需要注意的事情是,var
从PHP5开始就被弃用了。您需要根据需要使用public
、private
或protected
(编辑:当然,假设您使用的是PHP5)。创建一个帮助器类,该类可以满足您的需要:
class MyTask
{
private $task;
/**
* @var DateTime
*/
private $date1, $date2;
...
public function getTimeDiff() {
$hasDiff = $this->date1 && $this->date2;
if ($hasDiff) {
return $this->date2->getTimestamp() - $this->date1->getTimestamp();
} else {
return NULL;
}
}
public function __toString() {
return (string) $this->getTimeDiff();
}
/**
* @return \DateTime
*/
public function getDate1()
{
return $this->date1;
}
/**
* @param \DateTime $date1
*/
public function setDate1(DateTime $date1)
{
$this->date1 = $date1;
}
/**
* @return \DateTime
*/
public function getDate2()
{
return $this->date2;
}
/**
* @param \DateTime $date2
*/
public function setDate2(DateTime $date2)
{
$this->date2 = $date2;
}
}
这里的关键点是,该范围和内容的所有细节都在类中。所以你不需要关心其他地方
作为额外的好处,\uuuu toString
方法可以帮助您轻松地将此对象集成到视图中,因为您只需回显
对象即可
class Site extends CI_Controller
{
/**
* @var MyTask
*/
private $dates;
public function __construct() {
$this->dates = new MyTask();
parent::__construct();
}
function index()
{
$this->load->view('usability_test', $this->$dates);
}
...
更好?为自己创建一个帮助器类,它可以满足您的需要:
class MyTask
{
private $task;
/**
* @var DateTime
*/
private $date1, $date2;
...
public function getTimeDiff() {
$hasDiff = $this->date1 && $this->date2;
if ($hasDiff) {
return $this->date2->getTimestamp() - $this->date1->getTimestamp();
} else {
return NULL;
}
}
public function __toString() {
return (string) $this->getTimeDiff();
}
/**
* @return \DateTime
*/
public function getDate1()
{
return $this->date1;
}
/**
* @param \DateTime $date1
*/
public function setDate1(DateTime $date1)
{
$this->date1 = $date1;
}
/**
* @return \DateTime
*/
public function getDate2()
{
return $this->date2;
}
/**
* @param \DateTime $date2
*/
public function setDate2(DateTime $date2)
{
$this->date2 = $date2;
}
}
这里的关键点是,该范围和内容的所有细节都在类中。所以你不需要关心其他地方
作为额外的好处,\uuuu toString
方法可以帮助您轻松地将此对象集成到视图中,因为您只需回显
对象即可
class Site extends CI_Controller
{
/**
* @var MyTask
*/
private $dates;
public function __construct() {
$this->dates = new MyTask();
parent::__construct();
}
function index()
{
$this->load->view('usability_test', $this->$dates);
}
...
更好?你不理解错误的哪一部分?你不理解错误的哪一部分?@PeteJodo:没问题,我很高兴这有帮助!请参阅我上面的编辑,以获得其他一些提示/建议。@PeteJodo:没问题,我很高兴它能帮上忙!请参阅我上面的编辑,以了解其他一些提示/建议。