<?php
// Base class
class Vehicle {
protected $make;
protected $model;
protected $year;
public function __construct($make, $model, $year) {
$this->make = $make;
$this->model = $model;
$this->year = $year;
}
public function getInfo() {
return "{$this->year} {$this->make} {$this->model}";
}
public function startEngine() {
return "Engine started!";
}
}
// Derived class
class Car extends Vehicle {
private $numDoors;
public function __construct($make, $model, $year, $numDoors) {
parent::__construct($make, $model, $year);
$this->numDoors = $numDoors;
}
public function getInfo() {
return parent::getInfo() . " with {$this->numDoors} doors";
}
public function startEngine() {
return "Car engine purrs quietly";
}
}
// Another derived class
class Motorcycle extends Vehicle {
private $type;
public function __construct($make, $model, $year, $type) {
parent::__construct($make, $model, $year);
$this->type = $type;
}
public function getInfo() {
return parent::getInfo() . " ({$this->type})";
}
public function startEngine() {
return "Motorcycle engine roars loudly!";
}
}
// Using the classes
$car = new Car("Toyota", "Camry", 2022, 4);
echo $car->getInfo() . "<br>";
echo $car->startEngine() . "<br><br>";
$motorcycle = new Motorcycle("Harley-Davidson", "Street Glide", 2021, "Cruiser");
echo $motorcycle->getInfo() . "<br>";
echo $motorcycle->startEngine() . "<br>";
?>
Implementing inheritance and method overriding for polymorphic behavior.