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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:53:02+00:00 2026-05-23T01:53:02+00:00

I have started refactoring a small application to use a small DI container instead

  • 0

I have started refactoring a small application to use a small DI container instead of having
$registry::getstuff(); calls in my classes I inject them in a container.

This has raised 2 questions,

Q1 -> I extend Pimple DI class and create a container with dependencies specific to each object that will need DI. I then feed the object the whole shebang, and decrontruct it it in the constructor assigning the DI’s objects to the class properties of the object I’m building.

Should I be separating the object in the new object() call? I just found it easier like this but seeing I’m a one man team right now I just want to confirm I have proper methodology.

Q2 -> I find the $registry object I was passing around all over will be uneeded if I do this on a few of the main classes, is this a normal result of using DI, no more registry? I may have a singleton or two injected in the container but it looks as that is all I will need and even those could easily be eliminitated since the DI has a share() property that returns the same instance of the object, effectively removing the need for singletons. Is this the way to rid an app of needing registry/singletons, because if it is it’s darn easy like this.

  • 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-23T01:53:03+00:00Added an answer on May 23, 2026 at 1:53 am

    Q2 :
    If you were passing around all over your $registry object…. then your Registry was not really what is called a Registry (as Fowler described it).

    A Registry is more or less a global object (a “well-known”) with get/set methods.
    In PHP, two common prototypes for the implementations of a Registry would be

    As a singleton

    class RegistryAsSingleton
    {
        public static function getInstance (){
           //the singleton part
        }
    
        public function getStuff ()
        {
           //some stuff accessed thanks to the registry
        }
    }
    

    With static methods all over the place

    class RegistryAsStatic
    {
        public static function getStuff()
        {
        }
    }
    

    Passing your Registry all over the place makes it, well, just an object: a container with no greater purpose than providing references to other objects.

    Your DI container (using Pimple as you suggested in your OP) is kind of a Registry itself: It IS well known and enables you to get components from anywhere.

    So yes, we can say that your DI container will remove the requirement and necessity of a registry by performing the same functionality.

    BUT (there’s always a but)

    Registry are always guilty until
    proven innocent
    (Martin Fowler)

    If you’re using your DI Container to replace your Registry, this is probably wrong.

    eg:

    //probably a Wrong usage of Registry
    class NeedsRegistry
    {
        public function asAParameter(Registry $pRegistry)
        {
           //Wrong dependency on registry where dependency is on Connection
           $ct = $pRegistry->getConnection();
        }
    
        public function asDirectAccess ()
        {
           //same mistake, more obvious as we can't use another component
           $ct = Registry::getInstance()->getConnection();
        }
    }
    
    //probably a wrong replacement for Registry using DI Container
    class NeedsContainer
    {
        public function asAParameter(Container $pRegistry)
        {
           //We are dependent to the container with no needs, 
           //this code should be dependent on Connection
           $ct = $pContainer->getConnection();
        }
    
        public function asDirectAccess ()
        {
           //should not be dependent on container
           $ct = Container::getInstance()->getConnection();
        }
    }
    

    Why is this bad? Because your code is not less dependent than before, it still depends on a component (either a registry or a container) which does not provides a clear goal (we may think of interface here)

    The Registry-pattern be useful in some cases because it’s a simple and fairly inexpensive way to define components or data (e.g. global configuration).

    A way to refactor the above example without relying on DI by removing the dependency would be:

    class WasNeedingARegistry
    {
        public function asAParameter (Connection $pConnection)
        {
           $pConnection->doStuff();//The real dependency here, we don't care for 
           //a global registry
        }
    }
    
    //the client code would be like
    $wasNeedingARegistry = new WasNeedingARegistry();
    $wasNeedingARegistry->setConnection($connection);
    

    Of course, this may not be possible if the client code isn’t aware of the connection, which is probably the reason why you probably ended using a Registry in the first place.

    Now DI comes into play

    Using DI makes our life better because it will handle the dependency and enables us to access the dependency in a ready-to-use state.

    Somewhere in your code, you’ll configure your components:

    $container['connection'] = function ($container) {
        return new Connection('configuration');
    };
    $container['neededARegistry'] = function ($container) {
        $neededARegistry = new NeededARegistry();
        $neededARegistry->setConnection($container['connection']);
        return $neededARegistry;
    };
    

    Now you have everything you need to refactor your code:

    // probably a better design pattern for using a Registry 
    class NeededARegistry
    {
        public function setConnection(Connection $pConnection)
        {
           $this->connection = $pConnection;
           return $this;
        }
    
        public function previouslyAsDirectAccess ()
        {
           $this->connection->doStuff();
        }
    }
    
    //and the client code just needs to know about the DI container
    $container['neededARegistry']->previouslyAsDirectAccess();
    

    The “client” code should be as isolated as possible. The client should be responsible for and inject its own dependencies (via set- methods). The client should not be responsible for handling its dependencies’s dependencies.

    class WrongClientCode
    {
        private $connection;
        public function setConnection(Connection $pConnection)
        {
           $this->connection = $pConnection;
        }
    
        public function callService ()
        {
           //for the demo we use a factory here
           ServiceFactory::create('SomeId')
                           ->setConnection($this->connection)
                           ->call();
           //here, connection was propagated on the solely 
           // purpose of being passed to the Service
        }
    }
    
    class GoodClientCode
    {
        private $service;
        public function setService(Service $pService)
        {
           //the only dependency is on Service, no more connection
           $this->service = $pService;
        }
    
        public function callService ()
        {
           $this->service->setConnection($this->connection)
                         ->call();
        }
    }
    

    The DI container will configure GoodClientCode with the Service that has already been properly configured with its Connection

    As for the Singleton aspect, yes, it will enable you to get rid of them.
    Hope this helps

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

Sidebar

Related Questions

We have started to use spring aop for cross cutting aspects of our application
As part of my endless NHibernate-inspired DAL refactoring purgatory, I have started to use
I have started coding an FTP client application (for fun). I’m trying to represent
I have started design of a ColdFusion application that is entirely web based. Not
I've been refactoring Media Browser and have started to define a proper inheritance model
I have started to try to use StatET and Texlipse with a view to
I have started to use Microsoft LogParser in order to analyse IIS logs. LogParser
I have started to write a Python 3.x client application. The server application exists
I have started to test an application on Honeycomb and get the following error
I'm working on creating a dashboard. I have started to refactor the application so

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.