0
0
PHPprogramming~7 mins

Factory pattern in PHP

Choose your learning style9 modes available
Introduction

The factory pattern helps create objects without needing to know the exact class. It makes code easier to change and organize.

When you want to create objects but hide the details of how they are made.
When you have many similar classes and want a simple way to create them.
When you want to change the type of object created without changing the code that uses it.
When you want to keep your code clean and easy to maintain.
When you want to add new types of objects without changing existing code.
Syntax
PHP
<?php

class Factory {
    public static function create($type) {
        switch ($type) {
            case 'A':
                return new ClassA();
            case 'B':
                return new ClassB();
            default:
                throw new Exception('Unknown type');
        }
    }
}

class ClassA {
    public function say() {
        return "I am Class A";
    }
}

class ClassB {
    public function say() {
        return "I am Class B";
    }
}

The factory method is usually static so you can call it without creating the factory object.

Use a switch or if-else to decide which class to create based on input.

Examples
This creates an object of ClassA and calls its say method.
PHP
<?php
$obj = Factory::create('A');
echo $obj->say();
This creates an object of ClassB and calls its say method.
PHP
<?php
$obj = Factory::create('B');
echo $obj->say();
This shows how the factory handles unknown types by throwing an exception.
PHP
<?php
try {
    $obj = Factory::create('C');
} catch (Exception $e) {
    echo $e->getMessage();
}
Sample Program

This program uses the factory to create different vehicle objects. It calls their drive method to show what they do. If an unknown type is requested, it shows an error message.

PHP
<?php

class Factory {
    public static function create($type) {
        switch ($type) {
            case 'Car':
                return new Car();
            case 'Bike':
                return new Bike();
            default:
                throw new Exception('Unknown vehicle type');
        }
    }
}

class Car {
    public function drive() {
        return "Driving a car";
    }
}

class Bike {
    public function drive() {
        return "Riding a bike";
    }
}

try {
    $vehicle1 = Factory::create('Car');
    echo $vehicle1->drive() . "\n";

    $vehicle2 = Factory::create('Bike');
    echo $vehicle2->drive() . "\n";

    $vehicle3 = Factory::create('Plane');
    echo $vehicle3->drive() . "\n";
} catch (Exception $e) {
    echo $e->getMessage() . "\n";
}
OutputSuccess
Important Notes

The factory pattern helps keep your code flexible and easy to update.

It separates object creation from usage, so you can add new classes without changing much code.

Always handle unknown types to avoid errors.

Summary

The factory pattern creates objects without exposing the creation logic.

It helps manage many related classes with a simple interface.

Use it to keep your code clean and easy to maintain.