编辑代码

<?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();