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

The Archive Base Latest Questions

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

Comming from Java I feel more comfortable using Objects and Classes rather than plain

  • 0

Comming from Java I feel more comfortable using Objects and Classes rather than plain functions. The question is:
I currently have a ‘User’ class, which offers me methods such as getting access level, changing profile, erasing notifications, etc. Each method uses a query to my db to pull out the info needed or update it. But in the constructor I get the basic info: id, email, name and lastname (which are the fields I use the most on every method).

Which design would you use:
(Current)

class User{
   private $id;
   private $email;
   ...

   function __construct($id){
      //Connect to database
      ...
      $this->id=$id;
      $thid->name=$row['name'];
      ...

   }

   function getEmail(){
       return $this->email;
   }
  ...
}

Or:

class User{
   private $id;

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

   function getEmail(){
       //Connect to database using id
       return $row['email'];
   }
   ...
}

I feel that being just a couple of text fields the performance would not be improved by the second one, but I’m a begginer so I don’t know.

Thanks in advance

  • 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-23T22:04:37+00:00Added an answer on May 23, 2026 at 10:04 pm

    Do version 1 and cache the results of your db query into fields in your object. You don’t want to go to the db every time over the lifespan of the object and in a web app an object’s lifetime is only the single page request, so there is little chance of non-atomic data operation pollution.

    Further, don’t connect to the database in your constructor. Take the time to build a class factory to generate your classes. Honestly you can even write a very simple code generator to generate both the model class that stores all your data fields and the mapper class that goes to the db and builds your object from a table in under 100 lines of code.

    If you have the extra time, learn and use a framework like zend or for just data, doctrine…

    (FWIW, I think learning simple zend table stuff would net you a time savings)

    class User{
     protected $_id;
     protected $_email;
     protected $_username;
     protected $_userFirstName;
    
     public function __construct(){
     }
     public function setId($id){
       $this->_id = $id;
       return $this;
     }
     public function setEmail($email)
     {
       $this->_email = $email;
     }
      // etc and getters too
    }
    
    public class UserMapper
    {
      protected $_connection;
      public function __construct($connection){
        $this->_connection = $connection;
      }
      public loadUser($id){ 
        $select = $this->_connection->select(' * from Users')
                             ->where('id = ?', $id);
        $result = $select->fetchAll();
        //error checking here
        $user = new User();
        $user->setId($results[0]['id'])
             ->setEmail($results[0]['email'])
             ->setUsername($results[0]['username']);
        return $user;
      }
      public saveUser(User $user){
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am coming from Java and am currently working on a C# project. What
Coming from C++ to Java, the obvious unanswered question is why didn't Java include
I'm comming from Java world mainly. So, C# properties do look nice. I know
(Sorry for might be a trivial question , I'm coming from Java & Maven
Possible Duplicate: Python try-else Comming from a Java background, I don't quite get what
After comming from Ruby world, I'm having little problems doing TDD in Java. The
I'm using LEFT SHIFT operator from java to generate some unique number and validate
Coming from Java environment, I feel I got spoiled a lot when it comes
Coming from java/groovy/grails we are currently moving to rails. We have been having an
I am comming from the Java world where I usually worked with Eclipse and

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.