| 
	
 | 
 Posted by Jerry Stuckle on 12/01/07 02:12 
adam.timberlake@gmail.com wrote: 
> 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. 
>  
> 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. 
>  
> Thanks in advance. 
>  
 
A singleton is basically where you have a single object in the script. 
 
For instance - I use singletons a lot when working with databases. 
 
To keep it simple - let's say I have a database.  I have objects  
representing each table in the database (not really, but we're keeping  
it simple). 
 
Now, on any one page I may need to access 1 table, 5 tables, or anything  
in between.  I could create a database object on the page itself and  
pass it to each of the table objects, but that means I need to keep  
track of the database in each of my pages. 
 
Alternatively, I could get a database object for each table, but that  
would mean multiple connections in a single script. 
 
Or, I could create a singleton object, and have each of the tables use  
that singleton.  The web page script doesn't have to worry about it (in  
fact it doesn't even know about the database).  And I don't have  
multiple connections to the database. 
 
Does this help? 
 
--  
================== 
Remove the "x" from my email address 
Jerry Stuckle 
JDS Computer Training Corp. 
jstucklex@attglobal.net 
==================
 
  
Navigation:
[Reply to this message] 
 |