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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:04:27+00:00 2026-05-31T02:04:27+00:00

This is more of a pattern oriented question as opposed to a language-specific one.

  • 0

This is more of a pattern oriented question as opposed to a language-specific one. Suppose I have a User object that is of the form:

class User
    var id
    var email
    var password
    var dateCreated
end

Two common approaches that I’ve seen to persist a User object are as follows:

// Approach 1
new User(id)
    ->setEmail(email)
    ->setPassword(pass)
    ->setDateCreated(date)
    ->saveNew()

In this approach, the User object has each property set and then the saveNew method will build up the proper insert statement to execute on the database using the properties that were set on the User (e.g., insert into user values (this.email, ...). Another approach:

// Approach 2
new User()->saveNew(User userObj)

In this example, you see that the save method, although an instance method, is sort of being treated like a static method in that it is being passed a populated User object, not working on it’s own data, which in turn builds up the proper statement (e.g., insert into user values (user.email, ...)).

The second approach, to me, feels a little wrong. But, that’s why I’m asking you. As a continuation, let me ask you a similar question. Let’s say, we want to fetch a User. We can do something like the following:

new User(id)->fetch -or- new User()->fetch(id)

Now, in the fetch method, I’ve seen these 2 approaches:

function fetch
    result = db->fetch('select * from user where id=this.id')

    user = new User()
    user->setId(result->id)
    user->setEmail(result->email)
    user->setPassword(result->password)
    user->setDateCreated(result->dateCreated)

    return user
end

-and-

function fetch
    result = db->fetch('select * from user where id=this.id')

    // We can set the properties directly or through their 'setters'
    this->id = result->id
    this->email = result->email
    this->password = result->password
    this->dateCreated = result->dateCreated

    return this
end

Returning a new User object as opposed to setting the properties of this seems counter intuitive, but I see it places. Does all this matter? Is there a proper, pure way to do it?

  • 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-31T02:04:28+00:00Added an answer on May 31, 2026 at 2:04 am

    The second approach feels not a little wrong to me. If a method is used like a static method, it should be a static method, period. Creating an instance of a class with the sole purpose of being able to invoke a member method on it, which actually works on another, totally unrelated class instance passed as a parameter to it, is IMHO ugly, wasteful and hard to read at the same time.

    OTOH it is usually not the best idea to have the class bear the responsibility of persisting itself (apart from small and simple systems). This approach does not scale. If you have the persistence logic scattered all over the place, it will turn into a nightmare if the need arises to migrate to a different DB. Persistence is a concept orthogonal to the idea of representing a User, so it is best to handle it in a distinct class. Such classes are often called Data Access Objects (DAO). Using a DAO may look like this:

    dao = new UserDAO()
    ...
    user = new User(id)
    user->setEmail(email)
    user->setPassword(pass)
    user->setDateCreated(date)
    dao->save(user)
    
    ...
    
    user2 = dao->fetch(id2)
    

    This separation of concerns isolates your domain objects (entities) from the nitty gritty details of the persistence mechanism. This may enable you to switch to a different persistence provider, or even use multiple persistence mechanisms in parallel, without touching your domain objects. (OK, in practice it is not always so clear cut, but still this is usually a step in the right direction.)

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

Sidebar

Related Questions

This is more of a business-oriented programming question that I can't seem to figure
I guess this is more a design pattern question than a SQL one. In
this is more a design question. Suppose you have many beans A, B, C....
This is more of a generic regex question than a PHP-specific one. I am
This is more of a pattern question. I am using maven to create a
This is more of a question about best pattern and practice than asking about
Ok, I'm going to try to make this more clear because my last question
I have a label on my asp.net page, it looks like this: more info
This is more of an academic inquiry than a practical question. Are there any
This is more of an generic XML Schema question, but if and how do

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.