class CarFactory { public static function create($type) { $car = null; switch ($type) { case "sedan": $car = new Sedan(); break; case "suv": $car = new SUV(); break; case "sports": $car = new SportsCar(); break; } return $car; } } $myCar = CarFactory::create("sedan");
class OrderFactory { public static function create() { $customer = new Customer(); $order = new Order($customer); return $order; } } $order = OrderFactory::create();
class PaymentFactory { public static function create($type) { $payment = null; switch ($type) { case "paypal": $payment = new PayPalPayment(); break; case "stripe": $payment = new StripePayment(); break; case "braintree": $payment = new BraintreePayment(); break; } return $payment; } } $myPayment = PaymentFactory::create("paypal");The Model Factory pattern is a commonly used design pattern in PHP, and is typically included in many package libraries such as Symfony, Laravel, and CodeIgniter. It is used extensively in these frameworks to manage the creation of objects in a modular and extensible way.