Singleton Design Pattern in PHP : Implementation & Benefits Explained

Other Blogs

Blogs ❯❯ PHP

Image could not load

Singleton Design Pattern by KOBU Agency on Unsplash

जब हम software development करते हैं, कभी-कभी हमें ऐसे situation का सामना होता है जहाँ पर एक specific class का सिर्फ एक ही instance होना चाहिए। मतलब, पूरे application में एक ही object का use होना चाहिए, और उसको बार-बार create नहीं करना चाहिए।

ये वही case है जहाँ Singleton Design Pattern काम आता है।

Singleton pattern का main goal होता है एक class का एक ही instance create करना और उससे access करना। मतलब, जब भी आप उस class को access करते हैं, आपको वही एक instance मिलेगा।

Why Use Singleton Pattern?

  • Resource Management : अगर किसी class को बार-बार create किया जाता, तो वो memory और resources का ज़्यादा use करेगा। Singleton pattern से हम resource consumption को minimize करते हैं।

  • Global Access : हम single instance को globally accessible बना सकते हैं। मतलब, जो भी part of application को उस class कि जरूरत हो, वो उसी instance को use करेगा। 

  • Consistency : अब हालाँकि Singleton pattern से एक ही instance ensure होता है, जिससे पूरे application में data और state consistent रहती है, और आपको हर जगह same object मिलता है।

Singleton Design Pattern Example In PHP

class Singleton { // Static property to store the instance private static $instance = null; // Private constructor to prevent direct object creation private function __construct() { echo "Singleton Instance Created!"; } // Public static method to get the instance public static function getInstance() { if (self::$instance === null) { self::$instance = new Singleton(); } return self::$instance; } // Prevent clone object creation private function __clone() {} // Prevent unserialize object creation private function __wakeup() {} } // Accessing the Singleton class $instance1 = Singleton::getInstance(); // "Singleton Instance Created!" $instance2 = Singleton::getInstance(); // No new instance created // Checking if both instances are the same var_dump($instance1 === $instance2); // Output: bool(true)
Explanation :
  • Private Constructor : private function __construct() — इस used तो prevent direct instantiation of थे class. यह ensure करता है कि class का कोई भी object directly नहीं बन सकता।

  • Static Instance Property : private static $instance = null; — यह property एक static variable है, जिसमे हम class का single instance store करते हैं। 

  • Static Method : public static function getInstance() — ये method static है, जिसमे हम check करते हैं कि instance already exist करता है या नहीं , अगर नहीं, तो एक new instance create करते हैं। अगर instance already है, तो वही return होता है। 

  • Clone and Unserialize Prevention : हमने __clone() और __wakeup() methods को private बना दिया, ताकि कोई भी clone object या unserialize करके instance create न कर सके।

Key Benefits of Singleton In PHP

  1. Memory Efficiency : सिर्फ एक ही instance बनने कि वजह से आपकी application memory efficient होती है।

  2. Controlled Access : Since आप अपने class का instance control करते हो, आप easily उसकी behavior को manage कर सकते हैं। 

  3. Easier Debugging : आपको सिर्फ एक object देखना होता है, जो debugging को easy बनाता है। 

  4. Global Access Point : आप globally अपने instance को access कर सकते हो, जो के reusable और flexible है।

Drawbacks of Singleton Pattern

  • Testing Complexity : अगर आप unit testing कर रहे हो तो singleton classes को mock करना मुश्किल हो सकता है।

  • Hidden Dependencies : क्योंकि आप एक global instance use कर रहे होते हैं, तो dependencies को track करना थोड़ा tricky हो सकता है। 

  • Tight Coupling : Singleton pattern से class tightly coupled हो जाती है, जो कि future में flexibility को reduce कर सकती है।

When to Use Singleton Pattern In PHP?

  • Database Connections : अगर आपको database connection का एक ही instance हर जगह चाहिए हो, तो Singleton pattern use करके उसको manage कर सकते हैं।

  • Configuration Settings : अगर आप application के configuration settings को एक centralized location से manage करना चाहते हैं, तो Singleton pattern काफी useful हो सकता है। 

  • Logging Mechanism : अगर आपको application के लिए एक global logger maintain करना हो, तो आप Singleton pattern का use कर सकते हैं।

Conclusion

Singleton Design Pattern एक powerful tool है जो आपको एक ही instance का use करने कि facility देता है। इसका सही तरीके से use करना आपकी application को more efficient और manageable बना सकता है।

लेकिन, हमेशा ध्यान रखें कि design patterns को सिर्फ तब use करें जब उनकी जरूरत हो, क्योंकि हर pattern हर scenario में useful नहीं होता।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !