468,784 Members | 1,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,784 developers. It's quick & easy.

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.

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.

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.
Jan 14 '06 #1
9 2810
FluffyCat said the following on 14/01/2006 00:20:
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/


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
Jan 14 '06 #2
On 2006-01-14, Oli Filth <ca***@olifilth.co.uk> wrote:
FluffyCat said the following on 14/01/2006 00:20:
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/


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.


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>
Jan 14 '06 #3
> 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 "switchboard" 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
Jan 14 '06 #4
>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.


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

Jan 14 '06 #5
Gordon Burditt wrote:
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.


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

Oh you're gonna hate yourself in the morning... ;-)

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

-david-

Jan 14 '06 #6
On Sat, 14 Jan 2006 16:30:49 -0500, David Haynes
<da***********@sympatico.ca> wrote:
Gordon Burditt wrote:
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.


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

Oh you're gonna hate yourself in the morning... ;-)

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

-david-

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
Jan 15 '06 #7
On Sat, 14 Jan 2006 03:07:54 GMT, Oli Filth <ca***@olifilth.co.uk>
wrote:
FluffyCat said the following on 14/01/2006 00:20:
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/


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


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

Thanks!
Jan 15 '06 #8
On Sat, 14 Jan 2006 04:36:34 +0000 (UTC), Tim Van Wassenhove
<ti***@users.sourceforge.net> wrote:
On 2006-01-14, Oli Filth <ca***@olifilth.co.uk> wrote:
FluffyCat said the following on 14/01/2006 00:20:
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/


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.


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

That's an excellent point thanks!

I will add a note about that.
Jan 15 '06 #9
FluffyCat said the following on 15/01/2006 04:03:
On Sat, 14 Jan 2006 03:07:54 GMT, Oli Filth <ca***@olifilth.co.uk>
wrote:
FluffyCat said the following on 14/01/2006 00:20:
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/

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.


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


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
Jan 15 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alicia Roberts | last post: by
4 posts views Thread by Neil Zanella | last post: by
3 posts views Thread by Alicia Roberts | last post: by
11 posts views Thread by Daniel Billingsley | last post: by
13 posts views Thread by Robert W. | last post: by
2 posts views Thread by Kevin Newman | last post: by
3 posts views Thread by dischdennis | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.