<?phpnamespaceDesignPatterns\Creational\FactoryMethod;classItalianFactoryextendsFactoryMethod{protectedfunctioncreateVehicle(string$type):VehicleInterface{switch($type){caseparent::CHEAP:returnnewBicycle();caseparent::FAST:returnnewCarFerrari();default:thrownew\InvalidArgumentException("$type is not a valid vehicle");}}}

GermanFactory.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

<?phpnamespaceDesignPatterns\Creational\FactoryMethod;classGermanFactoryextendsFactoryMethod{protectedfunctioncreateVehicle(string$type):VehicleInterface{switch($type){caseparent::CHEAP:returnnewBicycle();caseparent::FAST:$carMercedes=newCarMercedes();// we can specialize the way we want some concrete Vehicle since we know the class$carMercedes->addAMGTuning();return$carMercedes;default:thrownew\InvalidArgumentException("$type is not a valid vehicle");}}}