Singleton Design in PHP

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

Are there any problems if my application accesses the database on several threads, for example, receiving false results?

Is the created instance per session or for all threads?

+4
source share
4 answers

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

+7
source

Unless you have some kind of weird frankenstein setup, all requests handled by php are independent and report nothing. Therefore, a singleton instance for each request (I think you are calling a thread).

Therefore, you do not need to worry about the user getting something that was intended for user B.

+5
source

PHP is single threaded. Every time a PHP script is executed, it starts from scratch. Objects created in the script are recreated every time.

+3
source

There should be no problems with multithreading, because PHP is not multithreaded.

0
source

Source: https://habr.com/ru/post/1385543/


All Articles