我创建了 Date() 类。但它没有提供所需的输出。
我的代码
<?php
class Date{
public $day = 1;
public $month = "January";
public $year = 2013;
public function __construct($dy, $mon, $yar){
$this->day = $dy;
$this->month = $mon;
$this->year = $yar;
}
public function lessthan($dt){
if($year != $dt->year)
return $year - $dt->year;
if($month != $dt->month)
return $month - $dt->month;
return $day - $dt->day;
}
public function pr(){
echo $day;
echo "<br>";
echo $month;
return;
}
}
$a = new Date(1,"January",2002);
$b = new Date(1,"January",2002);
$a->pr();
$b->pr();
echo "Hello";
?>
它只输出
[newline]
[newline]
Hello
我将 __construct() 更改为此
public function __construct($dy, $mon, $yar){
this->$day = $dy;
this->$month = $mon;
this->$year = $yar;
}
但输出仍然相同。错误是什么?
编辑:对不起我的错误。我输入了 this->$day 而不是 $this->day