<?php
interface Vehicle
{
public function start();
}
class Car implements Vehicle
{
public function start()
{
echo "Car starting.\n";
}
}
class Bike implements Vehicle
{
public function start()
{
echo "Bike starting.\n";
}
}
class VehicleFactory
{
public static function createVehicle($type)
{
switch ($type) {
case 'car':
return new Car();
case 'bike':
return new Bike();
default:
throw new Exception("Invalid vehicle type");
}
}
}
$car = VehicleFactory::createVehicle('car');
$car->start();
$bike = VehicleFactory::createVehicle('bike');
$bike->start();