tags:

views:

47

answers:

2

In PHP I know many people will use a class to SET and GET session variables, I am doing this now in many classes, I need to know if I am doing it wrong though.

So for example lets pretend I have A class that need to use this

$session->get('user_id')

Which gets this value

$_SESSION['user_id']

Now in this class if I have 15 methods and in each method I need to access this value several time, currently I am calling $session->get('user_id') 20 times in a class if it is needed 20 times, should I be setting this 1 time per class to a local variable for that class and then access it? I am not sure if it makes any difference or not, my theory is that the way I am doing it now is 20 extra function calls that could be avoided?

If my theory is correct, what would be the best way to store these values inside a class? Like a private or public or protected variable?

Thanks, sorry for any confusio, classes and objects are taking me a while to learn.

Also note that $session->get('user_id') is just 1 of many DIFFERENT variables I would need to do the same thing to as well.





UPDATE

After reading Chacha102's post about using an array() ... here is what I have tried, does this look like a good way or still can be improved a lot?

class file

<?PHP
class User
{
    // Load user details into an Array
    public function load_user()
    {
        $this->user_id = $this->session->get('user_id');
        //if user ID is already set, then Load the cached urser data
        if(isset($this->user_id) && $this->user_id != ''){
            // set user data to an array
            $this->user['user_id'] = $this->user_id;
            $this->user['user_name'] =  $this->session->get('user_name');
            $this->user['pic_small'] =  $this->session->get('pic_small');
            $this->user['sex'] =  $this->session->get('sex');
            $this->user['user_role'] =  $this->session->get('user_role');
            $this->user['location_lat'] =  $this->session->get('location_lat');
            $this->user['location_long'] =  $this->session->get('location_long');
            $this->user['new_user'] =  $this->session->get('new_user');
            return  $this->user;
        }
    }
}
?>

main page file

<?PHP   
require 'user.class.php';

$user = new User;

// if a user_id is set into a session variable then we return an array of other user related data
$user->account = $user->load_user();

// would show the user's ID from our array
echo $user->account['user_id'];
?>
+2  A: 

If you are doing something like this:

if($session->get('user_id')==1)
{
    $prefs = get_prefs($session->get('user_id'));
    $info = get_info($session->get('user_id'));
}

then I would replace it with a since local variable

$id = $session->get('user_id');
if($id == 1)
{
    //.....
}

It increases clarity for one. It probably isn't a big deal to call a simple function like that over and over again, but I still wouldn't do it.

I try to reduce the number of functions I call in a single method. If you are doing something like:

$user_id = $session->get('user_id');
$name = $session->get('name');
// ... etc ...

You might just want to grab an array of all the session variables instead.

$user = $session->get_array();
echo $user['user_id'];

This reduces the function calls, and you get all the data in one fell swoop.


Just one thing on clarity, using an array of user data is probably easier to read than to create a variable for each thing ($user_name, $user_id, etc).

Chacha102
I really like the idea of getting multiple values as an array, SO I would just need to build a function like get_array() and then inside it, make it fetch all the values I want right? Thanks
jasondavis
I have updated my question with some sample code based on an array, I am not sure if this is how to do it? It seems to work but is this a good way?
jasondavis
Your Session should just have a `get_all` function that the User Class Calls. It really shouldn't be nessecary to get each thing individually. Its already in an array (`$_SESSION`), so why not just grab that array.
Chacha102
Otherwise, that would probably be the next best way to get it. It does look kind of ugly tho.
Chacha102
Thanks you!! I did not think about that, that would be a lot easiar
jasondavis
I just remebered why I was not grabbing the _SESSION() array, I am wanting to keep it flexible, so that the data stored in my session object could actually be either a session, memcache, db result, or file cache result. So I am not sure if I could easily get the array if I ever switch to use something instead of $_SESSION
jasondavis
It would probably be fairly easy. But, if you switch to something like that you would want to still use the Session Interface, just look into `session_set_save_handler`
Chacha102
+2  A: 

For accesses distributed over a number of methods, as long as you're just using the function to access the variable, I'd say stay with the function. The additional cost is minuscule, and it's better for long term maintainability.

Within the same method, you would make one function call, populating a local variable, as Chacha102 suggests.

Even if the function does resource-intensive things like database calls, I would prefer giving the function some internal caching before adding a member to your class.

Adding the variable as a member to your class doesn't really make sense in the OOP way, because it's not a logical, legitimate member of the class but just a temporary variable.

Pekka
SO If I understand correctly, you suggest to call the function multiple times in 1 class but inside the classes methods, only call it once right? So if there is 10 methods in a class and each method called the function 5 times, instead of 50 calls to that function it would be like 10 calls?
jasondavis
Exactly. --------
Pekka