Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 557879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:04:23+00:00 2026-05-13T12:04:23+00:00

In PHP I know many people will use a class to SET and GET

  • 0

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'];
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:04:23+00:00Added an answer on May 13, 2026 at 12:04 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know in php you can embed variables inside variables, like: <? $var1 =
I know that this is a simple question for PHP guys but I don't
I know PHP scripts don't actually compile until they are run. However, say I
I know that PHP doesn't yet have native Enumerations. But I have become accustomed
First of all, I'm not looking for miracle... I know how PHP works and
I know that it does in PHP, and I'm pretty sure it does in
Anyone know of an opensource PHP Load Testing Framework similar to the Grinder http://grinder.sourceforge.net/
I know how to do a regular php mysql search and display the results.
PHP, as we all know is very loosely typed. The language does not require
I know how to disable WSDL-cache in PHP, but what about force a re-caching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.