Let me preface this with, I come from a Java background.
What is the scope of a static class member in PHP? ie: Request, Session, Server Lifecycle, etc
My current understanding is that everything is Request unless it is stuck on the session. I have found nothing in the Language Guide to refute or confirm this.
class MyKlass {
public static $K_PAGE_SIZE = 50;
public static $K_WITH_SPRINKLES = true;
}
if (isset($_GET['NO_SPRINKLES'])) {
MyKlass::$K_WITH_SPRINKLES = false;
}
var_dump(MyKlass::$K_WITH_SPRINKLES);
Case 1
If I were to visit a page with this code with nothing in the query line I should see
bool(true)
Case 2
If I were to visit the page with this code and ?NO_SPRINKLES=true
in the query line, I should see
bool(false)
Case 3
If I visit the page with ?NO_SPRINKLES=true
in the query line and then visit the page without it, I should always see bool(true)
right?
Case 4
After visiting the page with ?NO_SPRINKLES=true
, others who visit the page should still see bool(true)
correct?
PHP wont retain any information unless you make use of sessions. So a variable is created at the begining of the code when you're loading the page, and destroyed at the end of the code, when it's sent.
Same with classes and their members. You can edit them as much as you want, it'll retain information, but at the very end of your script, it'll be lost.
It's not about PHP, it's about HTTP Request-Response cycle. See, HTTP is specifically defined as a stateless protocol. That means each new request is processed as there was nothing before it (and will be nothing after, but that sounds too pessimistic).
Yes, there are several mechanisms of reducing this 'statelessness' - cookies, which are stored on the client-side; sessions, that use cookies or some param as keys to the information stored at the server-side. But in general your understanding is pretty correct: timespan of each entity used just by PHP (not stored in DB/file, or session) is just a Request.