1

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.

Example

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?

3
  • 1
    yes.. thats the way it should work... ofcourse if you did ?NO_SPRINKLES= you would also get false because youre testing for the presence of the key, not the value. Commented Jun 22, 2012 at 21:32
  • 1
    the time creating this well written question could have been invested in actually testing it ;) Commented Jun 22, 2012 at 21:36
  • 1
    @Dvir Azulay, absolutely true, but my purpose of posting was twofold. One, document to share with others. Two, see if anyone points to something in the documentation or offers a more detailed explanation than "this experiment passed my tests, so I'll assume it is true" Commented Jun 22, 2012 at 21:43

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks all! More good info from comments on raina77ow's answer "There is no Application Memory in PHP. Everything is created and destroyed with each Request. Even objects put into the Session are serialized and unserialized, so a static member you set in Request A will never be the same as the one in Request B" -@Gordon. AND "Yes, apache does keep a pool of PHP process to speed things up, but in theory they are all individual and "spawned and destroyed" as requests are received" - @Alex Belanger.
2

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.

16 Comments

Well it isn't just about the Request-Response cycle, it also depends on the server/container in which an app is deployed. In java, for instance, once the ?NO_SPRINKLES=true page was visited, the result would be bool(false) for everyone who visits that page until the server instance is restarted.
Look, HTTP is stateless by design: if you don't believe me, may be this doc be more convincing. ) Then again, that doesn't mean you cannot implement stateful systems on its base, but then you have to store these states somewhere.
Of course HTTP is stateless, that's not what I'm saying. There are some languages/environments where objects in memory are not. So for instance if I had a simliar setup on a WebLogic environment, I could have a static class called MyKlass. That object would stay in memory regardless of whether Requests are coming or not. If one Request causes that object to be changed, then the changed object stays in memory and is accessed by the next request. This next request will also see the change.
@hidden_premise: Yes, apache does keep a pool of PHP process to speed things up, but in theory they are all individual and "spawned and destroyed" as requests are received ;)
@hidden_premise your assumption is correct. There is no Application Memory in PHP. Everything is created and destroyed with each Request. Even objects put into the Session are serialized and unserialized, so a static member you set in Request A will never be the same as the one in Request B.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.