Analysis on the principle and application of PHP single element design mode

Source: Internet
Author: User

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. The PHP single-element design 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 design mode:

Listing 1. Singleton. php

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

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
  4. %

The two handles returned are the same object. If you use a database to connect to a single element in the PHP single element design mode, 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.