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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:52:23+00:00 2026-05-15T11:52:23+00:00

I have an object that loads an instance of another object in a method.

  • 0

I have an object that loads an instance of another object in a method.

$survey   = new survey;

$question = new question($survey);

$question->load($question_id);

class question  {

public function __construct(&$survey)
{   
    $this->survey = $survey;
}   

public function load ($id)
{   
    // now a question is loaded
    // want to load the survey that this question is in
    $this->survey->load($this->get('survey_id')); // ** survey_id is a field in the questions table
    // now $this->survey object has all the info about the survey this question is in
}   

private function get($name)
{   
    // returns $name, if isset() from array that load() sets
}    }

This is frying my brain, though, because it seems like $survey should come into $question already being a complete object. But, how do I do that, if I don’t know what survey_id to load until I’m in the object? I am revamping a site and the most complex part is littered with this issue. TIA – Hans.

  • 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-15T11:52:24+00:00Added an answer on May 15, 2026 at 11:52 am

    Edit: I just noticed you mentioned that you’re encountering this in existing code. What I’ve said below may still apply to solving your problem, but it might not actually be of immediate constructive use. 🙂 Unfortunately we’d need to see a lot more code to help you with your problem, as what you’ve shown us so far is bizarre.


    You seem to have constructed your classes inside out.

    Surveys are normally defined as sets of questions.

    Therefore, shouldn’t your Survey class contain and reference Questions, not the other way around?

    You can store their relationship in a link table, which will allow Surveys to look up their Questions, while also allowing Questions to look up which Surveys they’re in. Create accessor methods in each class to look up the dependency in the link table and return either IDs for the objects, or objects themselves. This method makes the objects less closely tied together.

    (Extending this to users answering questions, things get more interesting. If I was doing this, I’d create a class for each user’s Response (each user gets one Response), which contains Answers. The Response references the Survey, the Answers each reference a Question.)

    This probably isn’t a case where dependency injection is applicable. DI is most often used when the actual class of the object being passed may change, or when an outside force can control what object can be passed in. Do you ever expect that Surveys will contain things that aren’t Questions? Or that Questions will ever exist inside something that isn’t a Survey? Or that a force outside of either something related to Surveys or Questions will manipulate a Question? If you answered “no” to these, then DI is probably not what you want here.

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

Sidebar

Related Questions

I have written some code that loads an XML document using an XmlDocument object
I have the following code that loads a user's Active Directory DirectoryEntry object from
--Summary (shortened)-- I have a controller that loads a profile object from the corresponding
I have a Java Properties object that I load from an in-memory String ,
I have a class (ImageLoader) that extends Sprite container and loads an external image
I have a method that is suppose to edit a xml file: public void
For instance, I have a SqlDataSource that loads a list of items. On my
I have two tables: object that has object_id column and avalues that have object_id
I have an object that cannot be copied, a NetGame because it has a
I have an object that has pairs of replacement values used for simple encoding

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.