0

I am trying to create a function that clears a user ID session before a login is performed. You know the worst nightmare of a programmer is persistent sessions that remain open when another user logs in on the same machine. Assuming that this function will be called in a multi-user environment, can I define it as a static class function (which will have one instance only)

public static function clear()
{
 unset($_SESSION['user']
} 

or should it be declared as a object instance function?

    public function clear()
   {
     unset($_SESSION['user']
   } 

What will happen if more than one user invoke that function e.g. Logging in simultaneously? Will the sessions on all Client machines be deleted simultaneously which will of course be a chaotic outcome?

2
  • 2
    Sessions are per "user/browser instance": ie, each user has their own session..... static is static "per thread", not across all connections to a server. Each request to a server is unique and completely independent Commented Feb 1, 2014 at 17:58
  • What did you mean by multi-user environment? Do you want to know what happens if another user tries to login in the same browser where the previous one didn't logout? Commented Feb 1, 2014 at 17:58

1 Answer 1

1

Static methods aren't really OOP. They're just putting a load of procedural code in a class wrapper.

More pragmatically, static methods are difficult, if not impossible, to effectively unit test. They also introduce tight coupling and make substituting subclasses (which is one of the major points of OOP in the first place) very difficult.

I also think it might benefit you to get a better understanding of how sessions work in PHP. A session is a per-user construct. One user can't affect another user's session.

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

Comments

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.