我正在尝试使用基于外部文本文件的对象构建一个数组。但是类的构造函数并没有设置类的成员变量。相反,它使用值的名称声明新变量。
我究竟做错了什么?
输出
Array
(
[0] => slgDate Object
(
[date] =>
[day] =>
[info] =>
[13.01.2020] => 13.01.2020
[Mon] => Mon
[] =>
)
[1] => slgDate Object
(
[date] =>
[day] =>
[info] =>
[10.02.2020] => 10.02.2020
[Mon] => Mon
[] =>
)
//...
)
代码
class slgDate {
public $date, $day, $info;
function __construct($date, $day, $info = "Training") {
$this->$date = $date;
$this->$day = $day;
$this->$info = $info;
}
}
function getDates($f) {
$file = fopen($f, "r");
$dates = [];
while (!feof($file)) {
$fileGet = trim(fgets($file), "\r\n");
$expld = explode(';', $fileGet);
if (($date = strtotime($expld[0])) !== false) {
$info = $expld[1];
$strDate = date('d.m.Y', $date);
$strDay = date('D', $date);
$newDate = new slgDate($strDate, $strDay, $info);
array_push($dates, $newDate);
}
}
return $dates;
}
繁花不似锦