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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:26:52+00:00 2026-05-20T23:26:52+00:00

I’m trying to design a set of factory classes for our system, where some

  • 0

I’m trying to design a set of factory classes for our system, where some objects created by the factory also need to be initialized before they can be used properly.

Example:

$foobar = new Foobar();
$foobar->init( $qux, ... );
// $foobar ready for usage

For the same of example, lets say that the $qux object is the only dependency that Foobar needs. What I’d like to get to is:

$foobar = Foo_Factory( 'bar' );

In order to avoid the need to pass along the $qux object across the whole system and pass it to the factory class as another parameter, I’d like to perform initialization of Foobar directly in the factory class:

class Foo_Factory {

    public static function getFoo( $type ) {

        // some processing here
        $foo_name = 'Foo' . $type;
        $foo = new $foo_name();
        $foo->init( $qux );

        return $foo;
    }

}

There are few solutions that come to mind, but none of them is ideal:

  1. Add a static setter method for $qux to the factory class, and let it store a reference to $qux in a private static variable. The system can set $qux at the start, and the factory class can prevent any future changes (for security reasons).
    Although this approach works, using a static parameter for storing reference to $qux is problematic during unit testing (e.g. it happily lives survives between individual tests due to its static state).
  2. Create a new context class using Singleton pattern and let the factory class use it to get a reference to $qux. This might be a bit cleaner way to do this than option #1 (although we move the static problem from the factory class to the context class).
  3. Use dependency injection all the way, i.e. pass $qux to any object which uses the factory class, and let that object pass it along to the factory class as another parameter: Foo_Factory::getFoo($type, $qux);.
  4. Same as above (#3), but instead of passing $qux along the system, pass an instance of the factory class instead (i.e. in this case it would not be static, but instantiable).

What would you recommend please? Any of the four alternatives mentioned above, or is there a better way to do this please?

Note: I don’t want to get into a static is evil flamewar here, just trying to come up with the best solution.

  • 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-20T23:26:53+00:00Added an answer on May 20, 2026 at 11:26 pm

    I’d go with Dependency Injection all the way. But, instead of passing $qux along everywhere, just register it in the Dependency Injector Container and let the container sort it out. In Symfony Component speak:

    // Create DI container
    $container = new sfServiceContainerBuilder();
    
    // Register Qux
    $container->setService('qux', $qux);
    // Or, to have the DI instanciate it
    // $container->register('qux', 'QuxClass');
    
    // Register Foobar
    $container->register('foobar', 'Foobar')
              ->addArgument(new sfServiceReference('qux'));
    
    // Alternative method, using the current init($qux) method
    // Look! No factory required!
    $container->register('altFoobar', 'Foobar')
              ->addMethodCall('init', array(new sfServiceReference('qux')));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.