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

  • Home
  • SEARCH
  • 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 8088935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:14:21+00:00 2026-06-05T19:14:21+00:00

I am playing around with OOP programming, and have hit a hurdle I could

  • 0

I am playing around with OOP programming, and have hit a hurdle I could use advice with.

I have a situation where I have nested classes. I have a “Company” class, which contains an array called people. This array contains many instances of a “Person” class.

The data for both ‘company’ and ‘person’, are stored within a database, and the relevant class retrieves the information from that database as and when it is needed.

My question quite simply is this: “At what point do I connect to the database?” Do I:-
a) Connect first in my PHP page, then pass the connection to the instance of “Company” for it to use.
b) Put the username, password etc directly in the “Company” class and connect there.

I would have thought that the latter would create multiple connections to the database – one for each instance of “Company” – so probably the answer is “A”. But then, if I were to pass the class to another developer (this is only a learning exercise, so not something I plan to do), he would have to connect to the database himself, and not allow my class to do it for him.

In either case, would the connection be passed to each instance of the “Person” class automatically, or would I have to pass the connection each time I create a new instance?

I have done some research on this, and I think the answer is “A” (connect to the database in my PHP page, and pass the connection to each instance of the Company class), but just thought I should double check before I get too far.

Thanks for any advice you are able to offer.

  • 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-05T19:14:23+00:00Added an answer on June 5, 2026 at 7:14 pm

    I think you’re on the right track.

    What you’re describing is known as dependency injection and using it is generally a good idea.

    When you’re dealing with a more complex project, it might be a good idea to abstract the retrieval of data completely. Create an interface, i.e. “PersonFinder” or “CompanyFinder” that deals only with the lookup and retrieval of its associated records. Concrete implementations of this interface might include database lookups, or CSV file lookups, or even some sort of web-based service lookup. Pass your Finder into your Company or Person objects when you create them.

    This way you can easily change how you find your records without changing the code that deals with them.

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

Sidebar

Related Questions

Playing around with type-classes I came up with the seemingly innocent class Pair p
I am playing around with OOP in MATLAB, and I have the following constructor:
I'm just getting the hang of OOP and have been playing around with Java
Playing around with Python - tkInter - Entry widget - when I use validatecommand
Just playing around with interfaces and I have a question about something which I
I have been playing around with various methods of making the Visitor pattern in
I've been playing around with Simple.Data and have run across something that I can't
I'm just playing around with some code but it seemed to have the specific
Playing around with Google Maps these days, with some directions. I have a map
Playing around with shapeless natural numbers in excitement, I wonder what could be the

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.