Home  >  Article  >  Backend Development  >  PHP singleton pattern (Singleton Pattern) example tutorial

PHP singleton pattern (Singleton Pattern) example tutorial

WBOY
WBOYOriginal
2016-07-25 08:53:331056browse
  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 singleton mode.

  1. class DatabaseConnection
  2. {
  3. public static function get()
  4. {
  5. static $db = null;//Change the static members of Example 1 into static variables here
  6. if ( $db == null ){
  7. echo __LINE__;
  8. $db = new DatabaseConnection();
  9. }
  10. return $db;
  11. }
  12. private $_handle = null;//This will represent the static removal of Example 1
  13. private function __construct()
  14. {
  15. $dsn = 'mysql://root:password@localhost/photos';
  16. $this->_handle =123;
  17. }
  18. //A new method to get the private member $_handle is added here
  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] Of these two examples, my personal preference is the second one Four. Limit the number of instances that can be generated

  1. class DatabaseConnection {
  2. public static function get($persistent_id=0) {//Pass in an identifier
  3. static $db = array();//Change to array here
  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. //Add private member $_handle here Method
  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] In addition, using static method allows us to easily implement PHP's singleton mode. Of course it is also possible to use global variable storage, but this approach is only suitable for smaller applications. In larger applications, avoid using global variables and use objects and methods to access resources.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn