PHP Single-instance mode

Source: Internet
Author: User
Tags vars

Singleton pattern Concept Singleton mode refers to the design pattern of a class with only one object instance in the whole application. Characteristics of the Singleton model
    • A class has only one instance in the entire application
    • Class must create this instance on its own
    • This instance must be provided to the entire system on its own
The reason for using singleton mode in PHP I use PHP most of the operation is to deal with various databases, including Mysql,redis,memcache and other relational and non-relational database, so there will be a large number of connection database operations in an application, if not a singleton mode, It's always new, but each time new will consume a lot of memory resources and system resources, and each time you open and close the database connection is a great test and waste of the database. Post the bad database connection code I used to give you a demonstration of the error:
  1. <?php
  2. Class Mysqlconn
  3. {
  4. //MySQL database connection information
  5. Const MYSQLHOSTNAME = "127.0.0.1";
  6. Const Mysqlusername = "root";
  7. Const MYSQLPASSWORD = "* * *";
  8. Const MYSQLDBNAME = "Test";
  9. Const Mysqlcharset = "UTF8";
  10. /** 
  11. * Description:mysql Database connection function
  12. * Return value: Connection successfully returned to database connection handle; Connection failure returned error message
  13. */
  14. Public function Mysqlconnect ()
  15. {
  16. $db = new Mysqli (Self::mysqlhostname, Self::mysqlusernaem, Self::mysqlpassword, self::mysqldbname); //Connect to database
  17. $db->set_charset (self::mysqlcharset);
  18. if (Mysqli_connect_errno ())
  19. {
  20. throw New Circlemysqlexception ("Server system Failure", 1001);
  21. }
  22. Else
  23. {
  24. return $db;
  25. }
  26. }
  27. }

BUG: Each database connection should be new to this class, then call the Mysqlconnect method, return close, frequent new and database connection close operations are very resource-intensive

Improved:

Each time you should return directly to the database connection handle that is already open in the current app

    1. Singleton mode returns database connection handle
    2. $db = Mysqlconn::singlemysqlconnect ();
Implementation of PHP single-instance mode
  1. <?php
  2. Class Singleton
  3. {
  4. /** 
  5. * Description: (1) static variable, save global instance, bind to class, independent of Object
  6. * (2) Private attribute, in order to avoid calling class name directly outside of class:: $instance, prevent null
  7. */
  8. private static $instance;
  9. /** 
  10. * Description: Database connection handle
  11. */
  12. private $db;
  13. /** 
  14. * Description: Privatization of constructors to prevent external instantiation of objects
  15. */
  16. private static function __construct ()
  17. {
  18. }
  19. /** 
  20. * Description: Privatization of cloning functions to prevent external cloning of objects
  21. */
  22. Private function __clone ()
  23. {
  24. }
  25. /** 
  26. * Description: Static method, single access unified portal
  27. * @return Singleton: Returns the unique object instance in the app
  28. */
  29. public static function getinstance ()
  30. {
  31. if (! ( Self::$instance instanceof Self))
  32. {
  33. Self::$instance = new self ();
  34. }
  35. return self::$instance;
  36. }
  37. /** 
  38. * Description: Gets the connection handle of the private method of the database
  39. */
  40. Public function Getdbconnect ()
  41. {
  42. return $this->db;
  43. }
  44. }
    • Requires a static member variable that holds a unique instance of the class (usually $instance as a private variable)
    • Constructors and clone functions must be declared private, in order to prevent the external program new class from losing the singleton schema meaning
    • You must provide a public static method that accesses this instance, which returns a reference to the unique instance

PHP Single-instance mode

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.