Reputation: 3
I am trying to add a value to a public variable inside a class using a function. I am unsure how to do so. My current PHP code is as follows:
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
}
}
am attempting to add an integer value to $Price and $Horsepower through a method. I have attempted to use __construct() although this gives me a Fatal error: Cannot redeclare Truck::__construct().
Upvotes: 0
Views: 336
Reputation: 31749
You are defining two constructor
inside the class
, thus the error - Fatal error: Cannot redeclare Truck::__construct()
. Try -
class Truck {
public $Odometer = 0;
public $Model = 'Triton';
public $Price;
public $Horsepower;
public function __construct() {
$this->Price = 30;
$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
}
}
Upvotes: 2