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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:29:05+00:00 2026-05-14T02:29:05+00:00

I’m starting a new project and setting up the base to work on. A

  • 0

I’m starting a new project and setting up the base to work on. A few questions have risen and I’ll probably be asking quite a few in here, hopefully I’ll find some answers.

First step is to handle dependencies for objects. I’ve decided to go with the dependency injection design pattern, to which I’m somewhat new, to handle all of this for the application.

When actually coding it I came across a problem. If a class has multiple dependencies and you want to pass on multiple dependencies via the constructor (so that they cannot be changed after you instantiate the object).

How do you do it without passing an array of dependencies, using call_user_func_array(), eval() or Reflection? This is what i’m looking for:

<?php

class DI
{
    public function getClass($classname)
    {
        if(!$this->pool[$classname]) {
            # Load dependencies
            $deps = $this->loadDependencies($classname);

            # Here is where the magic should happen
            $instance = new $classname($dep1, $dep2, $dep3);

            # Add to pool
            $this->pool[$classname] = $instance;

            return $instance;
        } else {
                return $this->pool[$classname];
        }
    }
}

Again, I would like to avoid the most costly methods to call the class. Any other suggestions?

Also, how do I access the DI class inside classes, for example, in controllers that need to access different models? Should I call it statically or pass it along each class that would require it? I don’t think the last idea is feasible.

Thanks everyone.

  • 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-14T02:29:06+00:00Added an answer on May 14, 2026 at 2:29 am

    [Before I start, let me say that I’m mostly a Java programmer – with only a little bit of PHP knowledge. But I’ll simply try to get the most important concepts across without language specifics.]

    Dependency Injection is based on two parts of code:

    1. Construction
    2. Execution

    In its most extreme shape, there are no new operators to be found in the Execution part. All of them are moved into the Construction part. (In practice, this will be toned down.)

    All of the construction happens – in the Construction part. It creates the graph of objects needed for Execution bottom up. So let’s assume, it should construct A:

    • A depends on B, and
    • B depends on C.

    Then

    • C is constructed first.
    • Then B is constructed with C as a parameter.
    • Then A is constructed with B as a parameter.

    So C doesn’t have to be passed as a constructor parameter to A. This small example doesn’t illustrate strongly enough, how much this reduces the amount of objects that have to be passed around to quite a small number.

    The Dependency Injector itself should not be passed into the Execution part. This is one of the basic mistakes everyone (including myself) tries to make, when they first come in contact with DI. The problem is, that this would completely blur the lines between Construction and Execution. Another way to say it is, that it would violate the Law of Demeter. Or in pattern speak: It would eventually “degrade” the Dependency Injection pattern to the Service Locator pattern. It’s debatable, if this is really a degradation, but in any case it’s usually not a good idea to misuse the Dependency Injector as a Service Locator.

    So whenever you need to give one of your constructed objects the capability to produce other objects during execution, instead of passing the Dependency Injector, you would only pass simple Providers (a term used by the Java DI framework Guice). These are rather simple classes that can only create a certain kind of object. They have similarities with a factory.

    First try to pass the required dependencies directly to the constructor.

    So, to sum it up:

    • Build objects bottom-up.
    • Only pass as few dependencies as required to create an object.
    • Once your done, start executing.
    • During execution, you can still fetch newly created objects by using Providers.

    But don’t take it too far: Simple objects can still be created without a Provider 🙂

    And now, all you’ll have to do is to translate this stuff into quality code. Maybe others can help you out with a few PHP examples.

    Addendum: A little bit more about Providers

    As noted above, the notion “Provider” (a specialized factory) is a bit specific to the Java DI framework Guice. This framework can automatically create a Provider for any type of object. However, the concept is generally useful for DI. The only difference is, that without the help of Guice or a similar framework, you’ll have to write the Providers yourself – but that’s quite easy:

    Let’s say, B depends on C.

    • If B just needs one fixed instance of C, then you don’t need a Provider – you can simply construct B with the constructor argument C.
    • If B needs to create more instances of C during execution, then just write a class called CProvider with a get() method, that can create a new instance of C. Then pass an instance of CProvider into the constructor of B, and store the Provider in an instance field of B. Now B can call cProvider.get() when it needs a new instance of C.

    Providers are part of the Construction code, so you’re allowed to use new C(...)! On the other hand, they’re not part of the Execution code, so you shouldn’t have any execution logic there.

    CProvider can be passed into multiple constructors of course. You can also write multiple versions CProvider1, CProvider2, … – where each can construct different versions of C objects with different properties. Or you simple instantiate CProvider multiple times with different arguments.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.