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

The Archive Base Latest Questions

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

I have a question concerning dependency injection. I have been keeping it simple so

  • 0

I have a question concerning dependency injection. I have been keeping it simple so far, my methodology is basically to factor out object creation within objects and passing it instead in the constructer. I have come to a point where I am attacking larger classes that require multiple oblects. Some even have objects that contain other objects, with merry little singletons here and there. It gets ugly fast when testing these classes, as they are far from ‘isolated’ they are still hard-coded to their dependencies.

So. Injecting an object or 2 for a trivial class is straightforward,

I have looked into dependency containers, saw many implementations and am now wondering what is the advantage of using container vs. a registry for example. Couldn’t one just as easily use a registry to hold anonymous functions that create the needed dependencies when called upon?

The 2 containers I peeked into, Php Dependency and Pimple differ greatly in the implementation.

I am wondering on the advantages of user a container vs. passing straight objects. I fail to understand how php-dependency’s implementation would be tested, ie how would one implement the mock database object in phpunit without the actual class being injected when tested? Is there advantage to having dependency mapped out and used in doctags like this?

Class Book {

  private $_database;

  /**
   * @PdInject database
   */
  public function setDatabase($database) {
      $this->_database = $database;
  }

}

Pimple, on the other hand takes a totally different approach. No docblock tags, no mapping in seperate file, it seems like some kind of souped up registry ….

  Objects are defined by anonymous functions that return an instance of the object:

 // define some parameters
 $container['cookie_name'] = 'SESSION_ID';
 $container['session_storage_class'] = 'SessionStorage';

… that can behave as a factory at same time:

$container['session'] = function ($c) {
    return new Session($c['session_storage']);
};

Declaring shared ressources always serves the same instance (singleton!?):

$c['session'] = $c->share(function ($c) {
return new Session($c['session_storage']);
});

This is were I got the idea of using a simple registry that holds either objects or anonymous functions. BUt am I missing something in this approach? Pimple, I can see to how to test, but Php-Dependency is unclear to me from a testing point of view.

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

    Normally in our apps, we do constructor injection and define an interface for all components in our system:

    class Book
    {
        /**
         * @var Db_AdapterInterface
         */
        private $_database;
    
        public function __construct(Db_AdapterInterface $database)
        {
            $this->_database = $database;
        }
    }
    

    We have then of course a standard Db_Adapter and then another Db_TestAdapter. In Db_TestAdapter we can define results of SQL queries in our tests.

    For our normal app, we have something like this for our container:

    $container->add('Db_AdapterInterface', new Db_Adapter());
    

    And then in our tests, we have this line instead:

    $container->add('Db_AdapterInterface', new Db_TestAdapter());
    

    To get an instance of a Book, we simply ask the container for it:

    $book = $container->construct('Book');
    

    And the container injects all the needed dependencies into the object.

    If you keep all your objects loosely coupled, ie object A only needs an interface B then you can always provide object A with a test implementation. At that point what container you use doesn’t matter.

    We have a very simple IoC container which can do basic constructor injection. Our tests inherit from a base class which fills the container with standard test objects. That way we don’t have a lot of code just to construct an object we want to test.

    Update:
    I added an example of wiring things up in the container.

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

Sidebar

Related Questions

I have a question concerning this code which I want to run on QNX:
I have question concerning a function I created. I would like to show the
I have a question concerning unit testing. Let's say that I have several classes
I have a question concerning functions with jQuery. I have a function that once
I have a question concerning how the Unity container I have set up is
I have a question concerning volatile keyword I can't seem to find an answer
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
We have the question is there a performance difference between i++ and ++i in
We have a question with regards to XML-sig and need detail about the optional
I have a Question class: class Question { public int QuestionNumber { get; set;

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.