[英]Recursive object instantiation (in PHP)?
所以在我的 PHP 程序中,我正在创建一个日历功能,其中一个类是“CalendarDay”。 我想要做的是能够为每一天的计数实例化一个新的一天,例如new CalendarDay (22)
表示新的 22nd of the month date 。 还有一个show()
function 用于显示每一天。 class 本身运行正常,但是当我尝试使用递归实例化新的日子时,它似乎不再起作用,因为与实例化的 object 相关的所有内容都从网页上消失了。
class CalendarDay{
private $current_month;
private $current_year;
private $current_date;
public $reminderSet;
public $reminders;
public function __construct($current_day_of_month){
$current_year = date("Y");
$current_month = date("m");
$this->days_in_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);
$this->current_date = date("d");
$this->current_day_of_month = $current_day_of_month;
}
public function show(){
$output = '<div class = "generator>"';
//$output .= $this->current_date;
//$output .= '<h1>' . $this->current_date . '</h1>';
$output .= '</div>';
$output .= $this->current_day_of_month;
echo $output;
}
}
我的递归尝试失败:
for ($countTo31 == 0; $countTo31 == 31; $countTo31++){
$holder = $countTo31;
$date = new CalendarDay ($holder);
$date->show();
}
作为参考,这个没有递归的原始代码块可以正常工作:
$holder = $countTo31;
$date = new CalendarDay ($holder);
$date->show();
我对您要完成的工作感到非常困惑...
您有一个“天” class ,它接受输入来初始化特定的一天,但实际上是根据date("Ymd");
?..然后无论如何输出输入日期?
老实说,它看起来更像你想要一个“月” object
你使用==
来定义你的起点
==
不是赋值运算符,而是比较。
它在循环开始时有效地添加了循环的额外迭代
for($i == 1; $i < 5; $i++){ echo $i; } // Loop 1: // --> Notice on $i == 1 // --> Notice on $i < 5 // --> Notice on echo $i // --> Notice on $i++ // Loop 2: --> $i = 1 BECAUSE of the previous $i++ so the intended loop starts...
另外,在这种情况下,循环应该以 1 而不是 0(!)
您使用==
来定义您的条件
像你这样for
循环可以有效地工作:
for ( A; B; C ){ // Do something... } // Loop works as: // Check A against B // If TRUE then "Do something..." // and then do C // IF FALSE then break
但是,即使您的分配( A
)是正确的(即$countTo31 = 1
),它仍然无法工作,因为1 !== 31
并且因此循环从一开始就breaks
它应该是$countTo31 <= 31
循环object,重写变量
您的 HTML output 有一个"
在错误的地方:
$output = '<div class = "generator>"'; //Should be... $output = '<div class = "generator">';
class 中的某些变量未正确分配或声明
$current_month
已声明但从未分配$current_year
已声明但从未分配$current_day_of_month
已分配但未声明$days_in_month
已分配但未声明如果没有关于您打算做什么的进一步信息,就不可能提供良好/准确的指导,因此我将留下一个工作示例,它应该向您展示该怎么做:
$days_in_month = cal_days_in_month(
CAL_GREGORIAN,
date("m"),
date("Y")
);
for ($day = 1; $day <= $days_in_month; $day++){
echo "Day {$day}<br>";
}
对于您尝试实现的功能,您似乎并不真的想要“一天” class。 因此,在我看来,最好先创建一个包含该月所有日期的“月”object,然后为该月的每一天生成一个“日”object,然后可以收集每个月的信息一天例如提醒。
通过这种方式,您可以每天更新 go,例如用户输入或数据库数据。
class Month
{
private $month;
private $year;
private $days = [];
public function __construct($month, $year)
{
$this->month = $month;
$this->year = $year;
$number_of_days = cal_days_in_month(
CAL_GREGORIAN,
$month,
$year
);
for ($i = 1; $i <= $number_of_days; $i++){
$date = "{$this->year}-{$this->month}-{$i}";
// $days[] = new Day($date);
$this->days[$i] = new Day($date);
}
}
public function getDay($day)
{
return $this->days[$day];
}
public function getNumberOfDays()
{
return count($this->days);
}
}
class Day
{
private $date;
private $reminders = [];
public function __construct($date)
{
$this->date = $date;
// Initialise day...
# Get reminders
# Get meetings
# Get bills to pay
}
public function getReminders()
{
return $this->reminders;
}
public function setReminder($content, $time)
{
// Set reminders
$this->reminders[] = [
"content" => $content,
"time" => $time
];
}
public function show()
{
return date("d / m / Y", strtotime($this->date));
}
}
$month = new Month(12, 2020);
for ($i = 1; $i <= $month->getNumberOfDays(); $i++){
echo $month->getDay($i)->show()."<br>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.