Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton design in PHP

Tags:

php

I am using the singleton design pattern in a PHP application to create a database connection and select the database. I am using this instance many times in the application for CRUD operations.

Is there any problem if my application is accessing the database on multiple threads, such as getting unreliable results?

Is the created instance per session or for all threads?

like image 426
Surendra Royal Avatar asked Dec 06 '22 17:12

Surendra Royal


2 Answers

There are no threads in PHP. Each request start from scratch; objects and resources are not shared.

like image 179
sagi Avatar answered Dec 10 '22 12:12

sagi


Unless you have some weird frankenstein setup, all request processed by php are independent and do not share anything. Therefore, the singleton instance is per request (I think that's what you're calling thread).

So you should not have to worry about user A receiving something that was intended for user B.

like image 31
middus Avatar answered Dec 10 '22 11:12

middus