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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:43:18+00:00 2026-06-09T14:43:18+00:00

I have been practicing constructor dependency injection throughout my PHP application. I didn’t want

  • 0

I have been practicing constructor dependency injection throughout my PHP application. I didn’t want to be littering my code with object creation, so factories to the rescue, or at least I thought.

I set about wiring up components with factories, then some factories started using other factories to get dependencies, great, keeps all the creation code in one place. However, once factories start using each other (or as in the code below, itself) I ran into circular dependency issues, that simply cannot be resolved. For example, my MapperFactory uses itself to inject mappers with other mappers (they need each other to build a full object graph ‘eager loading’):

class MapperFactory
{   
    public function create($type)
    {
        switch (true) {
            case 'Item':
                $mapper = new ItemMapper(
                    $this->create('Field')  
                );               
                break;
            case 'Field':
                $mapper = new ItemMapper(
                    $this->create('Item')  
                );
                break;
            default:
                throw new Exception('Unknown mapper');
        }
        return $mapper;
    }

}

$mf = new MapperFactory();
$mf->create('Item');

Its a simplified example, but an increasingly common issue as the application is developing. Error back from PHP (xdebug installed) is:

Fatal error: Maximum function nesting level of '100' reached, aborting!

Fully understand why PHP is complaining (although didn’t see it coming TBH).

My question is, have I completely missed the point of factories? am I using factories correctly? It would seem not, but other than the circular dependency (pretty major but), factories are an elegant solution to hiding all the construction/wiring logic away from the main application.

  • 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-06-09T14:43:19+00:00Added an answer on June 9, 2026 at 2:43 pm

    You could try using a setter for injecting the dependencies. Then you’d create both mappers like this:

    $itemMapper = new Mapper();
    $fieldMapper = new Mapper();
    $itemMapper->setRelatedMapper($fieldMapper);
    $fieldMapper->setRelatedMapper($itemMapper);
    

    And then use the switch just to return the mapper. This should get rid of the circular dependencies when creating objects.

    Having said that, if you are doing this as a sort of OR/M thing to connect to a database, you should maybe look into stuff like Doctrine2 or Propel, just to save yourself the trouble of inventing the wheel when there are already tried and tested solutions out there.

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

Sidebar

Related Questions

I have been learning syntax for PHP and practicing it. I come from a
I have been practicing TDD and (some) XP for a few years now and
I have been practicing TDD in C# for several years now and recently have
I have been quite comfortable with windows-services, I have been practicing since from last
I've been practicing for an upcoming programming competition and I have stumbled across a
ListViews have always been my weak point and right now I am practicing putting
I've recently joined TopCoder and have been practicing in the Practice Rooms for the
I have been practicing my scraping abilities in Python. I have gotten pretty good,
All, I have been practicing coding problems online. Currently I am working on a
I'm practicing my gui skills with Java and I have been doing menus and

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.