The Singleton Pattern in PHP 5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FluffyCat

    The Singleton Pattern in PHP 5

    To add to my growing library of Design Patterns in PHP 5 I have
    written what I think is a good example of the Singleton Pattern.



    In the classic singleton pattern an object will distribute one and
    only one instance of itself. This can be useful for the sharing of
    resources such as a single db or network connection.

    A variation, sometimes called a multiton, would distribute a limited
    number of instances of itself. Useful, say if you had a limited
    number of db connections to share.
  • Oli Filth

    #2
    Re: The Singleton Pattern in PHP 5

    FluffyCat said the following on 14/01/2006 00:20:[color=blue]
    > To add to my growing library of Design Patterns in PHP 5 I have
    > written what I think is a good example of the Singleton Pattern.
    >
    > http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
    >[/color]

    Unfortunately, this is not a good example of a singleton system.

    You have a class (BookSingleton) with a public constructor, so there's
    nothing to stop someone creating multiple BookSingleton objects with
    multiple "new BookSingleton() " calls.

    To solve this problem, you need to make the constructor private, and the
    borrowBook() method static.


    P.S. Another thing to make your code more readable would be to syntax
    highlight it...


    --
    Oli

    Comment

    • Tim Van Wassenhove

      #3
      Re: The Singleton Pattern in PHP 5

      On 2006-01-14, Oli Filth <catch@olifilth .co.uk> wrote:[color=blue]
      > FluffyCat said the following on 14/01/2006 00:20:[color=green]
      >> To add to my growing library of Design Patterns in PHP 5 I have
      >> written what I think is a good example of the Singleton Pattern.
      >>
      >> http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
      >>[/color]
      >
      > Unfortunately, this is not a good example of a singleton system.
      >
      > You have a class (BookSingleton) with a public constructor, so there's
      > nothing to stop someone creating multiple BookSingleton objects with
      > multiple "new BookSingleton() " calls.
      >
      > To solve this problem, you need to make the constructor private, and the
      > borrowBook() method static.[/color]

      And make sure to throw an error when clone is called.. Because a singleton
      shouldn't be cloned :)

      --
      Met vriendelijke groeten,
      Tim Van Wassenhove <http://timvw.madoka.be >

      Comment

      • Dikkie Dik

        #4
        Re: The Singleton Pattern in PHP 5

        > Unfortunately, this is not a good example of a singleton system.

        I agree. Singletons are often used where, in procedural programming, one
        would use a global variable or function. Also, the root of a library or
        package can be a singleton. Examples:

        $connection = Database->OpenConnection (...);

        The Database class is here the root of the database package, and acts as
        the root of (or the gate to) the entire database functionality. There is
        no need to instantiate Database as such, because it is stateless and not
        really an instance. It is more the "switchboar d" that tells you where to
        find the database functionality. You might even argue that a singleton
        is something like a namespace.

        An example of a "global variable" is, for instance, Visual Basic's Err
        object, that is used for error handling. You could do that with a singleton:

        ErrorLog->AddMessage(... );

        The actual instance of the log object then remains "hidden" within the
        ErrorLog singleton. Naturally, you could also use the namespace-like
        structure:

        ErrorHandling->Log()->AddMessage(... );

        Where the log instance is visible, but should only be instantiated by
        the ErrorHandling singleton.

        Best regards

        Comment

        • Gordon Burditt

          #5
          Re: The Singleton Pattern in PHP 5

          >To add to my growing library of Design Patterns in PHP 5 I have[color=blue]
          >written what I think is a good example of the Singleton Pattern.
          >
          >http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
          >
          >In the classic singleton pattern an object will distribute one and
          >only one instance of itself. This can be useful for the sharing of
          >resources such as a single db or network connection.[/color]

          Why would one assume that there is only one instance of a database
          connection or only one network connection? Isn't that almost as
          bad as assuming that *THE ONLY* user presses *THE ONLY* key on
          *THE ONLY* keyboard with *THE ONLY* finger?

          Gordon L. Burditt

          Comment

          • David Haynes

            #6
            Re: The Singleton Pattern in PHP 5

            Gordon Burditt wrote:[color=blue][color=green]
            >> To add to my growing library of Design Patterns in PHP 5 I have
            >> written what I think is a good example of the Singleton Pattern.
            >>
            >> http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
            >>
            >> In the classic singleton pattern an object will distribute one and
            >> only one instance of itself. This can be useful for the sharing of
            >> resources such as a single db or network connection.[/color]
            >
            > Why would one assume that there is only one instance of a database
            > connection or only one network connection? Isn't that almost as
            > bad as assuming that *THE ONLY* user presses *THE ONLY* key on
            > *THE ONLY* keyboard with *THE ONLY* finger?
            >
            > Gordon L. Burditt
            >[/color]
            Oh you're gonna hate yourself in the morning... ;-)

            The classic database connection singleton is to a connection pool not a
            single connection.

            -david-

            Comment

            • FluffyCat

              #7
              Re: The Singleton Pattern in PHP 5

              On Sat, 14 Jan 2006 16:30:49 -0500, David Haynes
              <david.haynes2@ sympatico.ca> wrote:
              [color=blue]
              >Gordon Burditt wrote:[color=green][color=darkred]
              >>> To add to my growing library of Design Patterns in PHP 5 I have
              >>> written what I think is a good example of the Singleton Pattern.
              >>>
              >>> http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
              >>>
              >>> In the classic singleton pattern an object will distribute one and
              >>> only one instance of itself. This can be useful for the sharing of
              >>> resources such as a single db or network connection.[/color]
              >>
              >> Why would one assume that there is only one instance of a database
              >> connection or only one network connection? Isn't that almost as
              >> bad as assuming that *THE ONLY* user presses *THE ONLY* key on
              >> *THE ONLY* keyboard with *THE ONLY* finger?
              >>
              >> Gordon L. Burditt
              >>[/color]
              >Oh you're gonna hate yourself in the morning... ;-)
              >
              >The classic database connection singleton is to a connection pool not a
              >single connection.
              >
              >-david-[/color]


              Quite right, foolish of me to go out on a limb and get so darn and
              unnecessarily specific.

              The singleton pattern is (as described in GoF) "Ensure a class has
              only one instance, and provide a global point of access to it".

              Why you would or would not want such a thing I'll leave up to you.

              -Larry Truett

              Comment

              • FluffyCat

                #8
                Re: The Singleton Pattern in PHP 5

                On Sat, 14 Jan 2006 03:07:54 GMT, Oli Filth <catch@olifilth .co.uk>
                wrote:
                [color=blue]
                >FluffyCat said the following on 14/01/2006 00:20:[color=green]
                >> To add to my growing library of Design Patterns in PHP 5 I have
                >> written what I think is a good example of the Singleton Pattern.
                >>
                >> http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
                >>[/color]
                >
                >Unfortunatel y, this is not a good example of a singleton system.
                >
                >You have a class (BookSingleton) with a public constructor, so there's
                >nothing to stop someone creating multiple BookSingleton objects with
                >multiple "new BookSingleton() " calls.
                >
                >To solve this problem, you need to make the constructor private, and the
                >borrowBook() method static.
                >
                >
                >P.S. Another thing to make your code more readable would be to syntax
                >highlight it...[/color]

                Good call on the private constructor and static borrowBook() method,
                that really tightens the class up.

                Thanks!

                Comment

                • FluffyCat

                  #9
                  Re: The Singleton Pattern in PHP 5

                  On Sat, 14 Jan 2006 04:36:34 +0000 (UTC), Tim Van Wassenhove
                  <timvw@users.so urceforge.net> wrote:
                  [color=blue]
                  >On 2006-01-14, Oli Filth <catch@olifilth .co.uk> wrote:[color=green]
                  >> FluffyCat said the following on 14/01/2006 00:20:[color=darkred]
                  >>> To add to my growing library of Design Patterns in PHP 5 I have
                  >>> written what I think is a good example of the Singleton Pattern.
                  >>>
                  >>> http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
                  >>>[/color]
                  >>
                  >> Unfortunately, this is not a good example of a singleton system.
                  >>
                  >> You have a class (BookSingleton) with a public constructor, so there's
                  >> nothing to stop someone creating multiple BookSingleton objects with
                  >> multiple "new BookSingleton() " calls.
                  >>
                  >> To solve this problem, you need to make the constructor private, and the
                  >> borrowBook() method static.[/color]
                  >
                  >And make sure to throw an error when clone is called.. Because a singleton
                  >shouldn't be cloned :)[/color]


                  That's an excellent point thanks!

                  I will add a note about that.

                  Comment

                  • Oli Filth

                    #10
                    Re: The Singleton Pattern in PHP 5

                    FluffyCat said the following on 15/01/2006 04:03:[color=blue]
                    > On Sat, 14 Jan 2006 03:07:54 GMT, Oli Filth <catch@olifilth .co.uk>
                    > wrote:
                    >[color=green]
                    >> FluffyCat said the following on 14/01/2006 00:20:[color=darkred]
                    >>> To add to my growing library of Design Patterns in PHP 5 I have
                    >>> written what I think is a good example of the Singleton Pattern.
                    >>>
                    >>> http://www.fluffycat.com/PHP-Design-Patterns/Singleton/
                    >>>[/color]
                    >> Unfortunately, this is not a good example of a singleton system.
                    >>
                    >> You have a class (BookSingleton) with a public constructor, so there's
                    >> nothing to stop someone creating multiple BookSingleton objects with
                    >> multiple "new BookSingleton() " calls.
                    >>
                    >> To solve this problem, you need to make the constructor private, and the
                    >> borrowBook() method static.[/color]
                    >
                    > Good call on the private constructor and static borrowBook() method,
                    > that really tightens the class up.
                    >[/color]

                    Another point to make is that the returnBook() method is currently
                    pointless. Not only should it be static, but even if it were static, it
                    would serve no purpose.

                    Imagine the following code:

                    $book = BookSingleton:: borrowBook();
                    BookSingleton:: returnBook();

                    // umm, $book still refers to the BookSingleton instance...

                    To utilise a "returning" feature correctly, you would actually need to
                    hook onto the object destructor, rather than calling a method
                    explicitly. However, this is also impossible, as you maintain a static
                    reference to the object, so it will never get destructed.

                    Therefore, any attempt at "returning" the singleton object is futile.


                    --
                    Oli

                    Comment

                    Working...