SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question Singleton Design Pattern

    Hi PHP Gurus!!

    Can you please check and confirm if the following code snippet qualifies for a full or partial singleton design pattern?

    PHP Code:
    <?php
    Class Hello
    {
      private static 
    $MyObject null;

     static public function 
    listActiveUsers(){

      
    $userObj self::User();

     
    // code here....

     
    }

     static private function 
    User(){
       
        if(
    is_object(self::$MyObject)){
          return 
    self::$MyObject
       
    }

       return 
    self::$MyObject = new User();

     }

    }


    ?>


    Thanks in advance

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    TBH, I have no idea what you mean by "full" and "partial" singleton, but the code you're showing does adhere to the singleton pattern, yes.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for confirming that.

  4. #4
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Singleton pattern is very common, you don't need to worry about it.
    Quality codes are optimized and tested...
    Click here for inspiration..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •