PHP Design Patterns
In this page:
Design Patterns क्या हैं?
Design patterns recurring design problems के named, battle-tested solutions हैं -- वे developers को एक shared vocabulary देते हैं ('यहाँ बस एक Factory इस्तेमाल करो') बजाय सबके independently similar structures reinvent करने के।
उदाहरण: What are Design Patterns?
<?php
// Print "A named, reusable solution to a recurring design problem -- like 'use a Factory here'" to the output
echo "A named, reusable solution to a recurring design problem -- like 'use a Factory here'";
?>
Login to try C/C++/Java/PHP code in the editor
Singleton Pattern
Singleton pattern guarantee करता है कि एक class सिर्फ कभी एक बार instantiate हो सकती है और उस instance तक एक single global access point expose करता है, आमतौर पर एक shared database connection जैसी चीज़ों के लिए इस्तेमाल होता है।
उदाहरण: The Singleton Pattern
<?php
// Define the class `Database`
class Database {
// Declare the private static property `$instance`, set to `null`
private static $instance = null;
// Define the constructor `__construct` with no parameters
private function __construct() {}
// Define the function `getInstance` with no parameters
static function getInstance() {
// Check whether `self::$instance === null`
if (self::$instance === null) {
self::$instance = new self();
}
// Return `self::$instance` from this function
return self::$instance;
}
}
// Declare `$db1`, set to `Database::getInstance()`
$db1 = Database::getInstance();
// Declare `$db2`, set to `Database::getInstance()`
$db2 = Database::getInstance();
// Print a detailed dump (with types) of `$db1 === $db2`
var_dump($db1 === $db2);
?>
Login to try C/C++/Java/PHP code in the editor
Factory Pattern
Factory pattern object creation को एक dedicated method या class के पीछे छुपाता है, ताकि calling code 'एक Shape' माँगे बिना यह जाने कि उसे एक Circle या एक Square मिलेगा -- तब उपयोगी जब creation logic complex हो या input के हिसाब से vary करे।
उदाहरण: The Factory Pattern
<?php
// Define the interface `Shape` describing required methods
interface Shape {
// Define the function `draw` with no parameters
function draw();
}
// Define the class `Circle`, implementing `Shape`
class Circle implements Shape {
// Define the function `draw` with no parameters
function draw() { echo "Drawing a circle"; }
}
// Define the class `ShapeFactory`
class ShapeFactory {
// Define the function `create` taking `$type`
static function create($type) {
if ($type === "circle") return new Circle();
}
}
// Declare `$shape`, set to `ShapeFactory::create("circle")`
$shape = ShapeFactory::create("circle");
$shape->draw();
?>
Login to try C/C++/Java/PHP code in the editor
Observer Pattern
Observer pattern एक subject object को उसकी state बदलते ही registered observer objects की एक list को notify करने देता है, जो ज़्यादातर event-listener और pub-sub systems के पीछे की foundation है।
उदाहरण: The Observer Pattern
<?php
// Define the class `Subject`
class Subject {
// Declare the private property `$observers`, set to `[]`
private $observers = [];
// Define the function `subscribe` taking `$observer`
function subscribe($observer) {
$this->observers[] = $observer;
}
// Define the function `notify` taking `$event`
function notify($event) {
// Loop over `$this->observers`, binding each item to `$observer`
foreach ($this->observers as $observer) {
$observer($event);
}
}
}
// Create a new `Subject` instance, stored in `$subject`
$subject = new Subject();
$subject->subscribe(function ($event) {
// Print "Observer received: $event" to the output
echo "Observer received: $event";
});
$subject->notify("state changed");
?>
Login to try C/C++/Java/PHP code in the editor
Patterns के लिए Best Practices
Patterns real problems solve करते हैं लेकिन indirection add करते हैं -- एक five-line script पर एक Factory या Observer apply करना बिना किसी benefit के complexity add करता है, इसलिए किसी pattern तक तभी जाएँ जब किसी project की genuine needs इसे justify करें।
उदाहरण: Best Practices for Patterns
<?php
// A five-line script doesn't need a Factory or Observer -- that's added complexity with no benefit
function greet() {
echo "Hello!";
}
greet();
?>
Login to try C/C++/Java/PHP code in the editor
- जहाँ एक simple function काम कर जाता वहाँ एक design pattern apply करना, जो सिर्फ complexity add करता है।
- हर चीज़ के लिए Singleton इस्तेमाल करना, जो hidden global state बनाता है जिसे test करना मुश्किल है।
- classes को एक-दूसरे पर directly depend करवाना, जो Observer जैसे patterns का purpose defeat कर देता है।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser