【发布时间】:2016-03-04 10:01:42
【问题描述】:
我正在尝试使用函数向类中的公共变量添加值。我不确定该怎么做。我目前的PHP代码如下:
class Truck {
public $Odometer = 0;
public $Model = 'Triton';
public $Price;
public $Horsepower;
public function __construct() {
$this->Price = 30;
}
public function __construct() {
$this->Horsepower = 205;
}
public function ShowOdometer() {
echo "Odometer: ".$this->Odometer;
}
public function ShowModel() {
echo "Model: ".$this->Model;
}
public function ShowPrice() {
echo "Cost: ".$this->Price;
}
public function ShowHorsepower() {
echo "Horsepower: ".$this->Horsepower
}
}
我正在尝试通过一种方法将整数值添加到 $Price 和 $Horsepower。我尝试使用 __construct() 虽然这给了我一个致命错误:Cannot redeclare Truck::__construct().
【问题讨论】:
-
如错误所说
Cannot redeclare Truck::__construct()。一个类有一个construct()函数。 -
你不能有两次构造函数,否则一切正常。 (类中的变量也称为属性;函数称为方法)
标签: php function class variables