Reply to Re: Singletons in PHP

Your name:

Reply:


Posted by Willem Bogaerts on 11/30/07 13:41

> I was reading an article on TalkPHP (http://www.talkphp.com/
> showthread.php?t=1304) about singletons but I'm afraid I don't
> understand why I need to use them. I understand how to code them
> perfectly, it's just the theory I'm having some problems with. I did
> try searching on Wikipedia but it didn't yield any satisfactory
> reasoning, for me.

As with so many things, you don't really NEED them. Singletons are (or
are often abused as) global variables: You never know when these objects
are constructed, and have very little control over it. When it comes to
unit testing, they can be a real pain in, you know where.
The main problem is that they do not abide the Law of Demeter: they do
not come in through the interface of your objects.

> Could someone explain it to me in basic terms, please. I initially
> came across singletons when I was looking for ways in which to save on
> the extra memory of instantiating many of the same objects, and if I
> am right, a singleton would do just that? I'm just not sure how as it
> looks fairly straightforward in its implementation.

It is not the memory usage, I think. It is more that you would want only
one instance of an object with a "uniqueness guarantee". Like you do not
want two error logs to overwrite each other if they have taken the
filename from the same setting. You can, off course, put such an
instance on an Application object as well. It is a matter of taste.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация