开发者

Should a method ran several times in a PHP class be cached to a class variable instead?

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');
开发者_JAVA技巧            $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'];
?>


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜