Tutorial on SingletonPattern

Source: Internet
Author: User
Tutorial on SingletonPattern

  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. ?>

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

Example 2: php Singleton mode.

  1. Class DatabaseConnection
  2. {
  3. Public static function get ()
  4. {
  5. Static $ db = null; // The static member in Example 1 is changed to a static variable.
  6. If ($ db = null ){
  7. Echo _ LINE __;
  8. $ Db = new DatabaseConnection ();
  9. }
  10. Return $ db;
  11. }
  12. Private $ _ handle = null; // This parameter indicates static removal of Example 1.
  13. Private function _ construct ()
  14. {
  15. $ Dsn = 'MySQL: // root: password @ localhost/photos ';
  16. $ This-> _ handle = 123;
  17. }
  18. // The method for getting a 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. ?>

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

  1. Class DatabaseConnection {
  2. Public static function get ($ persistent_id = 0) {// input 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. // The method for getting a private member $ _ handle is added here.
  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. ?>

6 Handle = 123 6 Handle = 123 Handle = 123 [Finished in 0.1 s] Supplement: using static methods, we can easily implement the php Singleton mode. You can also use global variable storage, but 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.