PHP Singleton scope

Comments (3)

Shmulik in Kefar Habad, Israel

92 months ago

Might be a stupid question, but I really do not understand.
When creating a singleton pattern class,
it seems to me that on every request to the page, the instance should be recreated,
and I've expected that once I've used the singleton pattern and already created an instance of the class, this instance will be available for me through all pages / users requests.
Please explain me, what exactly I am missing here.


someone in Hemel Hempstead, United Kingdom

82 months ago

are you taking the piss? i don't think you should be a programmer.

Poncho in Fremont, California

75 months ago


Firsts off, that "someone in Hemel" is an idiot and obviously a pathetic loser.

I'm no expert on this, but this is how I understand it. When you use a singleton pattern class, let's say for a mysql connection, you do create a resource only once and use it for all the requests only in that page. When calling a lot of the mysql functions, the functions attempt to create a resource with each function call. For example if you have two inserts in the same page, you would create two resources. However, when using the singleton pattern you would create only one resource and reuse that resource for all inserts, deletions, updates, etc.. That is the point of singleton pattern.

The singleton pattern resource is open only for that page. The mysql connection closes at the end of that page. If you go to another page and mysql is used, another resource is created.

"this instance will be available for me through all pages / users requests. " - This is FALSE. The resource is only for you and only on that page.

» Sign in or create an account to comment on this topic.