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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:57:02+00:00 2026-05-22T19:57:02+00:00

I’ve been developing in PHP for a while now but only recently made the

  • 0

I’ve been developing in PHP for a while now but only recently made the switch over to an OOP approach.

One question that keeps cropping up for me is ‘how far’ to go with the OOP stuff, specifically in terms of execution speed and memory resources etc.

For example, let’s say I have 2 objects, User and Listing

Listings are always linked to a individual user. UserId is a property of Listing so I know which user it relates to.
Once in a while, within a Listing method I need to access a single property of the related User.

As far as I can see (please advise if not) I have 3 options for how to accomplish this.

  1. Create a new user object and access the relevent propery via $user -> theProperty

  2. Make the required property a local property of Listing and populate this when Listing is initialised (e.g. via a sql join)

  3. Query the database directly to retrieve the property of User required via the user ID

It seems to me that options 1 & 2 obide by OOP rules more stringently, but have performance hits due to initialising a whole object simply to retrieve 1 property. Option 3 would be the least memory intensive but sidesteps OOP altogether.

In addition, in terms of populating objects at creation, most of my objects get most of their properties populated via one ‘fill’ method shortly after being initialised (hence requiring only 1 DB query). Would this generally be considered best practise, or would it be more advisable to use individual methods to get these properties, populating as and when they are needed?

I realise there may not be “correct” answers to this, but could anyone give an advice on best ways to approach this situation?

Many thanks
Nick

  • 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-22T19:57:03+00:00Added an answer on May 22, 2026 at 7:57 pm

    I definitely prefer option 1. Option 2 does not follow the idea behind OOP because user information is not part of your listing, so keep it in a separate object.

    Keep the following rules in mind:

    1. Load an object without its relationships at once.
    2. If you need a related object load it at the time you need it according to rule 1

    Many ORMs work this way, for example Doctrine ( http://www.doctrine-project.org ). It’s called lazy loading.

    When you need one property of an object you will find yourself loading a second property of that same object some lines further. In many cases you will find yourself doing numerous database queries within a complete script for just one object.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.