我正在尝试使用基于外部文本文件的对象构建一个数组。但是类的构造函数并没有设置类的成员变量。相反,它使用值的名称声明新变量。我究竟做错了什么?输出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;}
添加回答
举报
0/150
提交
取消