PHP Classes & Objects
Defining a Class
class Car {
public $brand;
private $speed;
public function __construct($brand) {
$this->brand = $brand;
$this->speed = 0;
}
public function accelerate($amount) {
$this->speed += $amount;
}
public function getSpeed() {
return $this->speed;
}
}
Creating Objects
$myCar = new Car("Toyota");
$myCar->accelerate(30);
echo $myCar->getSpeed(); // 30
Inheritance
class ElectricCar extends Car {
public function charge() {
echo "Charging...";
}
}