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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:48:43+00:00 2026-05-27T01:48:43+00:00

I’m trying to figure out the best practices for dependency injection in PHP. Question:

  • 0

I’m trying to figure out the best practices for dependency injection in PHP.

Question: Do I have to inject all dependencies of a subclass into the parent class? I use the terms ‘parent’ and ‘child’ in terms of a composition relationship.

My hesitation is because I find myself giving the parent class all kinds of dependencies so that it can just pass them down to dependent child classes.

Edit:

Below is a more concrete example of what I’m talking about. MyClassA does not need the database connection object or the logger. DoSomething does need these objects, however. What is the best way to get the database connection and logger to the DoSomething instance? I don’t want to use singleton objects or global objects for the sake of unit testing. Also, this example only uses to classes. What if there are 3 or 4 and the 3rd or 4th needs some object instance but the first 2 or 3 don’t? Does MyClassA just pass the object to the next, and so on?

class MyClassA {
  protected $_doSomethingObject;

  public function doSomething()
  {
    return $this->_doSomethingObject()->doSomethingElse();
  }

  public function setDoSomethingObject($doSomethingObject)
  {
    $this->_doSomethingObject = $doSomethingObject;
  }
}

class DoSomething {
  protected $_logger;
  protected $_db;

  public function doSomethingElse()
  {
     $this->_logger->info('Doing Something');
     $result = $this->_db->getSomeDataById();
     return $results;
  }

  public function setLogger($logger)
  {
     $this->_logger = $logger;
  }

  public function setDBConection($db)
  {
     $this->_db = $db;
  }
}

Is the best way the example I show below? If so, then the best way is to work backwards so to speak…?

    $logger = new Logger();
    $db = new DBConnection();    

    $doSomething = new DoSomething();
    $doSomething->setLogger($logger);
    $doSomething->setDBConnection($db);

    $a = new MyClassA();
    $a->setDoSomething($doSomething);
  • 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-27T01:48:43+00:00Added an answer on May 27, 2026 at 1:48 am

    If so, then the best way is to work backwards so to speak…?

    Yes. As I mentioned in the comment, you set up the inner most objects first.

    If an object create another object internally that isn’t exposed, then it could pass along its injected objects if appropriate. For example:

    class DoSomething {
    // ...
      public function foo() {
        $foo = new Foo();
        $foo->setLogger($this->_logger);
        return $foo->bar();
      }
    }
    

    However, if that secret Foo object needed references to other things that DoSomething didn’t have, then you’ve got design issues. If that happens you need to do whatever is appropriate:

    • Inject a foo object into the parent object prior to calling foo().
    • Inject that dependency into the parent object prior to calling foo().
    • Add the dependency as a function argument.
    • Refactor code into a better design that doesn’t create that problem.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.