PHP Singleton mode (Singleton pattern) tutorial

Source: Internet
Author: User
    1. Class DatabaseConnection {
    2. private static $db;
    3. private static $_handle = null;
    4. public static function get () {
    5. if (self:: $db = = null) {
    6. Echo __line__;
    7. Self:: $db = new DatabaseConnection ();
    8. }
    9. return self::$_handle;
    10. }
    11. Private Function __construct () {
    12. $dsn = ' Mysql://root:password@localhost/photos ';
    13. Self::$_handle = 123;
    14. }
    15. }
    16. Print ("Handle =". Databaseconnection::get (). " \ n ");
    17. Print ("Handle =". Databaseconnection::get (). " \ n ");
    18. ?>
Copy Code

9Handle = 123 Handle = 123 [finished in 0.1s]

Example 2,php single-case mode.

    1. !--? php class databaseconnection
    2. {
    3. public static function get ()
    4. {
    5. static $db = null;//This changes the static member of Example 1 to a static variable
    6. if ($db = = null) {
    7. echo __line__;
    8. $db = new DatabaseConnection ();
    9. }
    10. return $db;
    11. }
    12. Private $_handle = null;//here will represent static removal of Example 1
    13. Private Function __construc T ()
    14. {
    15. $dsn = ' Mysql://root:password@localhost/photos ';
    16. $this->_handle =123;
    17. }
    18. //new method to get Private member $_handle
    19. public Function handle ()
    20. {
    21. return $this-> ; _handle;
    22. }
    23. }
    24. print ("Handle =". Databaseconnection::get ()->handle (). " \ n ");
    25. Print ("Handle =".) Databaseconnection::get ()->handle (). " \ n ");
    26. ?>
Copy Code

8Handle = 123 Handle = 123 [finished in 0.1s] These two examples of personal preference for the second four. Limit the number of instances that can be generated

  1. Class DatabaseConnection {
  2. public static function Get ($persistent _id=0) {//pass in an identity
  3. Static $db = Array ();//change here to array
  4. if (!array_key_exists ($persistent _id, $db)) {
  5. Echo __line__;
  6. $db [$persistent _id] = new DatabaseConnection ();
  7. }
  8. return $db [$persistent _id];
  9. }
  10. Private $_handle = null;
  11. Private Function __construct () {
  12. $dsn = ' Mysql://root:password@localhost/photos ';
  13. $this->_handle = 123;
  14. }
  15. Here's a new way to get private member $_handle
  16. Public function handle () {
  17. return $this->_handle;
  18. }
  19. }
  20. Print ("Handle =". Databaseconnection::get (1)->handle (). " \ n ");
  21. Print ("Handle =". Databaseconnection::get (2)->handle (). " \ n ");
  22. Print ("Handle =". Databaseconnection::get (2)->handle (). " \ n ");
  23. ?>
Copy Code

6Handle = 123 6Handle = 123 Handle = 123 [finished in 0.1s] supplement, using the static Statics method allows us to easily implement PHP singleton mode. Of course, you can also use global variable storage, but this approach is only available for smaller applications. In larger applications, you should avoid using global variables and use objects and methods to access resources.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.