0

I have used SLIM framework in PHP to implement some REST APIs. All the REST API code is in index.php, where I am using a global variables. Each of the REST API function sets a global variable, and another child function reads it. Now the reason to choose global variable is to not pass this variable from one function to another, nothing else.

I wanted to know if there could be concurrency problem, where two REST requests interleave? Is it that a single object of this index.php (OOPS) is instantiated for all the requests?

Is this how this sequence of event look like?

Request 1 -> Set global var to '10'
Request 2 -> Set global var to '9'
Request 1 -> Reads global var as '9'
Request 2 -> Reads global var as '9'
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Ouroboros
  • 1,432
  • 1
  • 19
  • 41

1 Answers1

0

No, it's not an issue. In PHP, global variables are unique to the specific request. You cannot alter or read variables from another request, except by using sessions.

Matt Raines
  • 4,149
  • 8
  • 31
  • 34
  • What about it MPM module is used? Still the same answer? – Ouroboros Jun 21 '15 at 14:40
  • Any link on the same which details out such things would be helpful. – Ouroboros Jun 21 '15 at 14:48
  • There's good advice on thread safety in PHP in [this question](http://stackoverflow.com/questions/1623914/what-is-thread-safe-or-non-thread-safe-in-php), but generally the advice seems to be **not** to use a multi-threaded MPM. I can't see anything explicit in the manual about variables not spanning requests, but it's implied by [Variable scope](http://php.net/language.variables.scope), [Thread safety](http://php.net/manual/en/internals2.memory.tsrm.php), [Data persistence](http://php.net/manual/en/internals2.memory.persistence.php), and [Session](http://php.net/manual/en/intro.session.php). – Matt Raines Jun 21 '15 at 16:46