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 7781419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:13:18+00:00 2026-06-01T19:13:18+00:00

<?php class User { private $id; private $username; public function __construct($id = null) {

  • 0
<?php

class User {

    private $id;
    private $username;

    public function __construct($id = null) {
        $this->id = $id;

        if (!is_null($this->id)) {
            $this->load();
        }
    }

    public function load() {


    }

}

?>

In the ‘load’ method I am going to load all the information from the current user (id) But I wonder how is the best way to load all info. I could grab all data and then just assign all private variables, but I assume there must be another “cheaper” way to get all the data so I can use as such

$this->variable;

And not have to ASSIGN every single data row, I select in the load method. How?

  • 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-06-01T19:13:19+00:00Added an answer on June 1, 2026 at 7:13 pm

    I am assuming the following:

    • Each User object represents 1 row in your database
    • You can retrieve information for your user in an associative array:

      $user = array('name' => 'John', 'age' => 20);

    Then perhaps using variables variables could be a viable solution in your load() method:

    foreach($user as $key => $userData){
       $this->$$key = $userData; //Note the use of variables variable
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i created a model object in PHP class User { public $title; public function
the class, which is content.php class content { private $vars = array(); public function
Here is the class: functions.php class buildPage { public function Set($var,$val){ $this->set->$var = $val;
New to PHP. I have created a simple class: class User { private $name;
PHP PDO Singleton Class: <?php require_once('app/config.php'); // Require constants HOST, DATABASE, USER, PASSWORD /*
I'm using this php class ( http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php ) for creating the different sized images
I have a PHP class with a method. In the base class (it's more
I'm not sure if this is a trivial questions but in a PHP class:
Here is the basic structure of my User class: class User { private $_userId
I ran into this serialize gotcha today. From PHP.net doc: Note: Object's private members

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.