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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:06:13+00:00 2026-06-13T08:06:13+00:00

With regards to using class objects within another class what is the best practice?

  • 0

With regards to using class objects within another class what is the best practice? To pass the class objects in the class _construct statement or create a new class object?

Example 1:

class Foo {
    private $bar;

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

Or Example 2 :

class Foo {

    private $bar;

    public function __construct(){
        $this->bar= NEW bar;
    }    
}

I’m aware that obviously it’s taken for granted that the class file must already be included somewhere else, and in the first instance a class object of this type would need to exist already, but I want to know what the advantages are each method are, as I have a lot of classes I need to code that use a database object and I need the best way to pass this into the classes. Is there a third option that’s better than these two?

From what I can understand, the advantage of the first one could be a few less lines of coding and in the case of a DB, not having a new connection created. The second one might be better however because it’s more self contained? Anyhow I thought I’d ask the experts.

  • 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-13T08:06:14+00:00Added an answer on June 13, 2026 at 8:06 am

    The first. (This approach is called Dependency Injection).

    The constructor asks for whatever the object in questions needs in order to work. This way, it’s pretty clear from the methods alone (what they need, and what they return), what it does. Without even looking at the source code.

    A way to improve your code would be to introduce type hinting into your method:

    class Foo {
        private $bar;
    
        public function __construct(Bar $bar){
           $this->bar = $bar;
       }
    }
    

    So that only Bar objects may be passed in.


    Advantages of Dependency Injection

    • Very readable.
    • Ability to tell the method’s dependencies without viewing the source code.
    • Makes Unit Testing possible.
    • *Saves kittens from God’s wrath.

    * Disclaimer: No kittens were harmed during the manifestation of this answer

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

Sidebar

Related Questions

I bumped into an additional question that I needed in regards to this: Using
In regards to: Find Hyperlinks in Text using Python (twitter related) How can I
Is it possible to extarct a .zip file using Zlib1.dll in the c#/.Net? Regards
Are there any issues, with regards to efficiency, for using a function call in
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if
I have a question with regards to automapping entity framework objects which have been
I know i can convert JSON to an object using HJackson. Below is the
I'm using the MapMaker to implement caching of data objects in my application: public
So I'm trying to teach myself object oriented programming using VB.net, and Although I
My first question is that if I write a class in C++ using opencv

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.