Beispiel #1
0
 public function testCreate()
 {
     $this->assertInstanceOf('Car', CarsFactory::create('bmw'));
     $this->assertInstanceOf('Car', CarsFactory::create('mercedes'));
     $this->assertInstanceOf('Car', CarsFactory::create('ferari'));
     $this->setExpectedException('CarException');
     CarsFactory::create('susita');
 }
    }
}
class BMW extends Car
{
    public function getMaxSpeed()
    {
        return 260;
    }
    public function getWeight()
    {
        return 2500;
    }
}
class Lada extends Car
{
    public function getMaxSpeed()
    {
        return 100;
    }
    public function getWeight()
    {
        return 3000;
    }
}
$carsFactory = new CarsFactory();
$cars['toyota'] = $carsFactory->createCar('toyota');
$cars['bmw'] = $carsFactory->createCar('bmw');
foreach ($cars as $mod => $obj) {
    echo 'Max speed: ' . $mod . " = " . $obj->getMaxSpeed();
    echo ' Weight: ' . $mod . " = " . $obj->getWeight() . '<br>';
}
Beispiel #3
0
<?php

require_once "car.php";
try {
    $myCar = CarsFactory::create('bmw');
    echo $myCar->getDetails();
    //will throw exception since this car not exists
    //$myBmw = CarsFactory::create('susita');
} catch (CarException $e) {
    echo $e->getMessage();
}