Design patterns are standard solutions to common problems in software design. They help make code more reusable and easier to understand. In this tutorial, we’ll cover several popular design patterns used in PHP.
PHP Design Patterns
1. Singleton Pattern
The Singleton pattern restricts a class to a single instance and provides a global point of access to it.
class Singleton {
private static $instance;
private function __construct() {
// Private constructor to prevent direct object creation
}
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
// Usage
$singleton = Singleton::getInstance();
2. Factory Pattern
The Factory pattern is used to create objects without specifying the exact class of the object that will be created.
interface Shape {
public function draw();
}
class Circle implements Shape {
public function draw() {
echo "Drawing a Circle";
}
}
class Square implements Shape {
public function draw() {
echo "Drawing a Square";
}
}
class ShapeFactory {
public static function createShape($type) {
if ($type == "circle") {
return new Circle();
} elseif ($type == "square") {
return new Square();
}
return null;
}
}
// Usage
$shape = ShapeFactory::createShape("circle");
$shape->draw(); // Output: Drawing a Circle
3. Observer Pattern
The Observer pattern is a behavioral design pattern that defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
class Subject {
private $observers = [];
public function attach($observer) {
$this->observers[] = $observer;
}
public function notify() {
foreach ($this->observers as $observer) {
$observer->update();
}
}
public function changeState() {
// Change state and notify observers
$this->notify();
}
}
class Observer {
public function update() {
echo "Observer notified!";
}
}
// Usage
$subject = new Subject();
$observer = new Observer();
$subject->attach($observer);
$subject->changeState(); // Output: Observer notified!
4. Strategy Pattern
The Strategy pattern enables selecting an algorithm's behavior at runtime. It defines a family of algorithms and makes them interchangeable.
interface PaymentStrategy {
public function pay($amount);
}
class PayPal implements PaymentStrategy {
public function pay($amount) {
echo "Paid $amount using PayPal";
}
}
class CreditCard implements PaymentStrategy {
public function pay($amount) {
echo "Paid $amount using Credit Card";
}
}
class ShoppingCart {
private $paymentStrategy;
public function setPaymentStrategy(PaymentStrategy $strategy) {
$this->paymentStrategy = $strategy;
}
public function checkout($amount) {
$this->paymentStrategy->pay($amount);
}
}
// Usage
$cart = new ShoppingCart();
$cart->setPaymentStrategy(new PayPal());
$cart->checkout(100); // Output: Paid 100 using PayPal
5. Conclusion
Design patterns are essential tools in PHP that help developers create efficient, maintainable, and scalable code. Understanding these patterns will improve your software design skills and help you tackle complex programming challenges.