What is the purpose of using the Factory Pattern in PHP programming?

The Factory Pattern in PHP is used to create objects without specifying the exact class of object that will be created. This allows for flexibility in object creation and helps to maintain a separation of concerns in the codebase. By using the Factory Pattern, developers can easily add new types of objects without needing to modify existing code that relies on object creation.

interface Shape {
    public function draw();
}

class Circle implements Shape {
    public function draw() {
        echo "Drawing a circle\n";
    }
}

class Square implements Shape {
    public function draw() {
        echo "Drawing a square\n";
    }
}

class ShapeFactory {
    public function createShape($shapeType) {
        switch ($shapeType) {
            case 'circle':
                return new Circle();
            case 'square':
                return new Square();
            default:
                throw new Exception("Invalid shape type");
        }
    }
}

$factory = new ShapeFactory();
$circle = $factory->createShape('circle');
$circle->draw();

$square = $factory->createShape('square');
$square->draw();