Code Analysis Principle of PHP single element mode

Source: Internet
Author: User

Everyone has always had a wrong idea that the design mode is only prepared for the Java architect. In fact, the design pattern is very useful to everyone. Here we will introduce the single element pattern of PHP. After reading this article, you will surely gain a lot.

Some application resources are exclusive because there is only one resource of this type. For example, the connection from a database handle to a database is exclusive. You want to share the database handle in the application, because it is an overhead when you keep the connection open or closed, especially when you get a single page. PHP single-element mode can meet this requirement. If an application contains only one object each time, the object is a Singleton ). The code in Listing 1 shows a single database connection element in PHP V5.

Case study of PHP single element mode:

Listing 1. Singleton. php

 
 
  1. <?php 
  2. require_once("DB.php");  
  3.  
  4. class DatabaseConnection  
  5. {  
  6. public static function get()  
  7. {  
  8. static $db = null;  
  9. if ( $db == null )  
  10. $db = new DatabaseConnection();  
  11. return $db;  
  12. }  
  13.  
  14. private $_handle = null;  
  15.  
  16. private function __construct()  
  17. {  
  18. $dsn = 'mysql://root:password@localhost/photos';  
  19. $this->_handle =& DB::Connect( $dsn, array() );  
  20. }  
  21.  
  22. public function handle()  
  23. {  
  24. return $this->_handle;  
  25. }  
  26. }  
  27.  
  28. print( "Handle = ".DatabaseConnection::get()->handle()."n" );  
  29. print( "Handle = ".DatabaseConnection::get()->handle()."n" );  
  30. ?> 

This code displays a single class named DatabaseConnection. You cannot create your own DatabaseConnection because the constructor is dedicated. However, with the static get method, you can obtain only one DatabaseConnection object. In the two calls, the database handle returned by the handle method is the same, which is the best proof. You can run the code in the command line to observe this point.

 
 
  1. % php singleton.php  
  2. Handle = Object id #3  
  3. Handle = Object id #3  

The two handles returned are the same object. If you use a single database connection element in the entire application, you can reuse the same handle anywhere. You can use global variables to store database handles. However, this method is only applicable to small applications. In large applications, 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.