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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:28:30+00:00 2026-05-29T10:28:30+00:00

PUBLIC SERVICE UPDATE: I’ve learned a lot since I originally posed this question. If

  • 0

PUBLIC SERVICE UPDATE:

I’ve learned a lot since I originally posed this question. If you’re reading this, please take my advice and avoid static altogether. Just. Don’t. Use. It. There is no way to dependency injection; dependency injection is the way.


I’ve recently spent a lot of time digging into various Inversion of Control (IOC) concepts. I totally agree with those who believe a Service Locator is an anti-pattern. I built one to tinker with and was aghast at the power it allowed for importing “global” entities in the middle of classes using static locator methods as well as the possibility for hiding the actual dependencies of an object.

Moving on from the service locator I set out to create a Dependency Injection (DI) container that gave me the flexibility of static dependency access without the concomitant drawbacks of static variables.

Here’s a simple example of such an implementation:

<?php

class Container
{
  protected static $params = [];

  public function store($key, $val)
  {
    static::$params[$key] = $val;
    return $this;
  }

  public function fetch($key)
  {
    if (isset(static::$params[$key])) {
      return static::$params[$key];
    }
    $msg = "No parameter match found in container: $key";
    throw new OutOfBoundsException($msg);
  }
}

$container = new Container;
$container->store('widgetDep', new WidgetDependency);
$container->store('kumquatDep', new KumquatDependency);

// and somewhere else in the application without access to the global namespace
// (i.e. the $container instance we just created) ...

$widget  = new Widget(new Container);
$kumquat = new Kumquat(new Container);

This seems a step in the right direction because the static $params property is protected and no static methods exist to access or manipulate it in a “global” static scope: an object requires access to the container to access dependencies.

Oh, wait …

Unfortunately, storing dependencies in this container means that now every dependency-injected object has a faux-dependency on the container object, thus hiding its real dependencies. Another negative side-effect would be that every object would be given access to every available dependency in the container, and obviously, a Widget object shouldn’t have access to a Kumquat object’s dependencies. Also, using an abstract factory with this approach does nothing but move the fake dependency out of the Widget and Kumquat classes and into the factory.

A PHP 5.4 alternative

With 5.4’s new object construction dereferencing capabilities, we could do something like the following without needing access to the already created $container instance that exists in the global namespace:

$widget  = new Widget((new Container)->fetch('widgetDep'));
$kumquat = new Kumquat((new Container)->fetch('kumquatDep'));

Using this approach we’ve successfully:

  1. Eliminated the container dependency from the Widget and Kumquat objects, allowing their constructors to typehint the specific dependency objects they require;
  2. Prevented the downstream Widget and Kumquat objects having access to dependencies they shouldn’t know exist;
  3. Retained static dependency storage capabilities.

Now, a possible drawback is that this approach means the developer must be disciplined enough to not pass a full Container object as a dependency. This is crucial.

So the question is …

In two parts:

  1. What concrete drawbacks do you see with this approach, and
  2. Is the static Container::$params even necessary? Should it instead be a standard protected property accessed by top-of-the-object-graph factory classes/methods in the global namespace anyway (obviating the need for static)?
  • 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-29T10:28:31+00:00Added an answer on May 29, 2026 at 10:28 am

    You shouldn’t use static here at all. Just create a container: $container = new DIContainer(); and use that object as a typical dependency. After all there’s a very few places in the core of application that require access to the whole container.

    Take a look at Symfony’s Dependency Injection component – piece of quite good code.


    EDIT:

    According to the first comment. Yes, you’ve misunderstood me. Usually you’d need only several dependencies from the container, so you’ll write something like:

    $service = new Service($container->get('dep.a'), $container->get('dep.b'), 123);
    

    My point was that you shouldn’t use static property within the container, as it makes it nothing more but a global object. There would be no difference between:

    global $container;
    $widget  = new Widget($container->fetch('widgetDep'));
    $kumquat = new Kumquat($container->fetch('kumquatDep'));
    
    $widget  = new Widget(Container::getInstance()->fetch('widgetDep'));
    $kumquat = new Kumquat(Container::getInstance()->fetch('kumquatDep'));
    
    // You're using new objects but they share the same, **global** array.
    // Therefore, they are actually global themselves.
    $widget  = new Widget((new Container())->fetch('widgetDep'));
    $kumquat = new Kumquat((new Container())->fetch('kumquatDep'));
    

    In other words, the Container itself should be a local variable, and if you’ll need to access it somewhere else (some objects might need access to the entire container) then you should explicitly pass it as dependency to that object.

    As I said before, take a look at Symfony DIC and the whole framework to see how to make a good, well-written DIC.


    Simple container:

    class Container {
        private $services = array();
    
        public function get($service) {
            if (!array_key_exists($this->services, $service)) {
                throw ...;
            }
    
            return $this->services[$service];
        }
    }
    
    $containerA = new Container();
    $containerB = new Container();
    
    // $containerA and $containerB are completely different 
    // objects and don't share anything
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update 18th December 2012 Since this question seems to be getting quite a few
i have this thread which run as a service: public void run() { try
I have this code that describes a service: public class navigation_web extends Service {
i have this on my rest service public List<Invoice> Invoices { get; set; }
This is what I Have in my WCF service public long Generic_Save(Product p, ObjectSet
we have a public asmx service that is accessed by our clients through SOAP
I have a simple WCF service: public Order[] GetOrdersByStatus(int statusid) { OrderService os =
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I need help with wcf service. i have a ajax-enabled wcf service: public class
Is there a public/government web service that I can call to find out what

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.