0

I need to redesign the back-end of a website and need some help getting started. The original website uses OOP, there is a class "User" used to saved data when users log on. This is serialized and unserialized on each page as needed:

// login.php
$user = new User($iduser);
// ...
// At the end of the script
$_SESSION["user"] = serialize($user);

// Another page
$user = unserialize ($_SESSION["user"]);

Thus that data is available on every page that the user visits. Plus, in (almost) every page visited, $_SESSION["user"] is updated with new data (it’s quite possible that page_a.php doesn’t use data that page_b.php needs to show).

The new design should be fully object oriented. And the main problem I find is that I don’t know how to store properly that $_SESSION["user"] in a class method.

I tried to use Codeigniter, but it uses session variables with cookies, and that is not what I need.

Any suggestion or link would be helpful. Thanks in advance.

4
  • 1
    PHP also uses a cookie when you use $_SESSION. Anyway, can't you write a class that wraps $_SESSION? The calls themselves won't be OOP, but you can hide all non-OOP calls (to other functions as well) in classes. Commented Jul 13, 2015 at 17:51
  • 2
    What is the question here? $_SESSION is a superglobal. So you can "write" to it wherever you want. It does not matter if that is within a class method or a function in the global namespace. Commented Jul 13, 2015 at 17:52
  • I am agree with @arkascha Commented Jul 13, 2015 at 18:02
  • 1
    I know that $_SESSION can be used everywhere, but I don't want to use it in ALL my classes, that's a really bad practise. What would happen if php changes the way sessions work or the name of the superglobal? I (or the next programmer) would have to change a lot of lines in the code. Commented Jul 14, 2015 at 10:14

1 Answer 1

2

I have no experience with Codeigniter, so I can't give any specific advise on that. I trust others will point you in the right direction if there are any specific tools available for this use case in this framework. I can give you some insights on how you could handle this with basic, vanilla PHP tough.

If you want to keep things simple, I would suggest working with some sort of App singleton (or Auth, or whatever you want to call it), that can handle static data like the active user. Consider the following (very basic and untested) code

class App {

    protected static $instance;
    public $user;

    final private __construct() {
        $user = unserialize($_SESSION['user']);
    }
    final private __clone() {}

    public static getInstance() {
        if (! self::$instance) {
            self::$instance = new static;
        }
        return self::$instance;
    }

    public function __destruct() {
        $_SESSION["user"] = serialize($this->user);
    }
}

This would allow you to do things like:

/* fetch the active user */
$user = App::getInstance()->user;

/* set the active user */
$app = App::getInstance();
$app->user = new User($iduser);

The destruct handler takes care of sending your data back to the session when you are done with the App object. And since we use the singleton pattern, there can only be one instance of it, no matter where you call it from. This ensures you are always talking to the same User.

Let me know if anything is unclear, or if you want me to explain further.

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

3 Comments

Yeah! Thanks a lot, that's exactly what I was looking for. Just one more question. I don't want to use singleton pattern here because the original code uses more than one session var (3, in fact, depending on what pages are visited). This code is a complete disaster! What should I change?
Well, it's hard to tell without seeing any of the code obviously. And I know the singleton pattern is not always ideal since it introduces tight coupling and makes your application hard to test. Dependency injection is the obvious alternative, but then I would advise going for a framework in stead of building everything from the ground up (I'm a Laravel fan myself). Sticking with the example in my answer, why not just add those 2 extra vars onto the App singleton? Singleton means there can only be one App instance, but the App class can obviously contain more then just that user...
I'll try to add 2 extra vars like you suggest, I think it'll work. Thanks a lot again!

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.