0

I am new to PHP trying to migrate from Java to PHP. This question may be a foolish one. Sorry if it is.

I have found few singleton implementations in PHP and I am planning to use it for database. My question is: What will happen if two different PHP pages are called or a page is called by more than one client at the same time ? Will it not use the same db connection, statement and store result in same variable ? Wont the result get overridden and corrupted ?

mrd081
  • 279
  • 2
  • 11
  • 2
    Every PHP page has its own variables and resources. – knittl Aug 02 '12 at 10:30
  • but I am using singleton class. – mrd081 Aug 02 '12 at 10:36
  • @mrd081 That is irrelevant. It's only a singleton within the scope of the currently executing script. That's per-user, per-connection - every concurrent execution environment gets its own memory space. There is zero danger of overlap. – DaveRandom Aug 02 '12 at 10:40
  • haa haa haa ... thats completely different from Java ... Good to know that. I guess I will make all my variables static so that they will be shared across all instances and make functions to get connection. hope that will serve my need. – mrd081 Aug 02 '12 at 10:44
  • See this thread answer http://stackoverflow.com/questions/2082223/keeping-db-connection-active-across-pages – Oussama Jilal Aug 02 '12 at 10:44

1 Answers1

1
  • No, every request is separated from each other. This means especially: Every resource (including database connections) is closed at the end of the request. However, Usually nothing to worry about
  • No, concurrent access to the database doesn't hurt it. In fact thats one reason, why databases are popular compared to flat files ;)
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • but i am using singleton class. if everything is separate then whats the use of singleton ? – mrd081 Aug 02 '12 at 10:36
  • First of all: Forget the singleton-pattern. It is evil! It's just a pattern, to ensure, that from a given type (class) only a single instance exists. That's all. And regarding "separated": Imagine you restart your computer. Everything is gone, right? Nothing else happens to processes, when they were shutdown, and nothing else (at least "nearly nothing else") is a request hitting the PHP-runtime. – KingCrunch Aug 02 '12 at 10:38
  • I want to keep all my database related variables at one place. how can I do that ? Can I simple create a class constants having all static constants ? – mrd081 Aug 02 '12 at 10:41
  • @mrd081 I can understand why you're getting confused coming from a Java background, you need to remember that PHP is a completely different beast. It is a scripting language, it doesn't do multi-threading or anything like that, it just deals with one client at a time. All the concurrency and that sort of complicated stuff is handled by the overlying environment (e.g. Apache). Imagine a Java application where you started a whole new instance of it for each client that connects - that's how PHP works. Sort of. It's certainly a good way to think of it for now. – DaveRandom Aug 02 '12 at 10:44
  • means simplest way is to define it in a file and include it in every page required instead of getting into design pattern mentality :) – mrd081 Aug 02 '12 at 10:49
  • @mrd081 Design patterns are not bad (even in PHP ;)), but singleton is more an anti-pattern (at least in PHP) nowadays: At all it's only a cool-looking way to hide global variables and nobody (at least in PHP) likes global variables (or at least "should like" ;)). – KingCrunch Aug 02 '12 at 11:19