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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:01:40+00:00 2026-05-13T13:01:40+00:00

The Singleton and the Registry patterns were very simple and easy for me to

  • 0

The Singleton and the Registry patterns were very simple and easy for me to understand right away but the Factory pattern has been something I haven’t been able to get my brain to interpret 100% yet. I think I might understand it now, I have wrote a sample code below, please review and tell me if this is the proper use of the Factory pattern. Sample is in PHP…

<?php
 /**
 *   Factory.class.php
 */
class Factory {
    public static $_database;
    public static $_cache;
    public static $_session;

    // Build our User object with all it's dependencies  
    public static function makeUserObject()
    {
        $user = new User();
        $user->setDatabaseObject(self::$_database);
        $user->setCacheObject(self::$_cache);
        $user->setSessionObject(self::$_session);
        return $user;
    }

    // other objects will be here someday......
}

/**
 *  User.class.php
 */
class User
{
    public function __construct() { }

    // inject Database Object
    public function setDatabaseObject($databaseConnectionObject)
    {
        $this->_databaseObject = $databaseConnectionObject;
    }

    // inject Cache Object
    public function setCacheObject($cacheObject)
    {
        $this->_cacheObject = $cacheObject;
    }

    // inject Session Object
    public function setSessionObject($sessionObject)
    {
        $this->_sessionObject = $sessionObject;
    }

    // other methods here for User object...........
}

/**
 *  index.php  Main page that puts it all together
 *  assume that classes are autoloaded into this page already
 */
// Set our Database + Cache + Session objects into the Factory Object
Factory::$_database = new Databse();
Factory::$_cache = new Cache();
Factory::$_session = new Session();

// Create our User object
// The factory class will build the User object and inject all
// it's dependencies for us =)
$user = Factory::makeUserObject();

?>

So basicly the Database, Cache, and Session objects are created (not shown here) then they are added to the Factory object, I the can build a method in the factory class for each object that will need any of these 3 dependencies and I can set which ones they get too. This also makes it where the individual classes can still be somewhat portable as I can directly inject there dependencies if I wanted to without the factory object. Does this sound right? If this is right, this sounds really useful


UPDATE # 1

This is based off this here a blog post I read here http://www.potstuck.com/2009/01/08/php-dependency-injection/ they refer to it as a “factory”, I been using a Registry and a lot of people keep telling me to look into a “factory” and everything I read about it just didn’t click in my head until I read this artcle but looks like it isn’t a “factory”?


UPDATE # 2
From wikipedia http://en.wikipedia.org/wiki/Factory_object
In object-oriented computer programming, a factory object is an object for creating other objects. It is an abstraction of a constructor, and can be used to implement various allocation schemes, such as the singleton pattern.
A factory object typically has a method for every kind of object it is capable of creating. These methods optionally accept parameters defining how the object is created, and then return the created object.
Factory objects are used in situations where getting hold of an object of a particular kind is a more complex process than simply creating a new object. The factory object might decide to create the object’s class (if applicable) dynamically, return it from an object pool, do complex configuration on the object, or other things.

So maybe this is a “Factory Object” in a way afterall…

  • 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-13T13:01:40+00:00Added an answer on May 13, 2026 at 1:01 pm

    Summarized and extended my comments from below the question here

    Like others said, it is not a Factory, simply because a pattern with this name does not exist. It’s either an AbstractFactory or a FactoryMethod, though pragmatically people often refer to either or by just saying Factory and that’s fine with me.

    Session, Cache and DB are usually something you will initialize early in your application flow, so this is basically bootstrap work. I get the impression what you are looking for is not so much the creation of objects, but their handling throughout the application. That’s a somewhat different concern from what a FactoryWhatever does.

    Like I said in the comments, just because it isnt exactly a FactoryWhatever, doesn’t mean that your code is bad. If it solves your problem, it’s cool. But I still think, what you are trying to do, e.g. creating and managing resources at runtime is best used with a DI Service Container.

    If you don’t want to use a DI Container now for this, you could have a look at Zend_Application and how they bootstrap resources. It’s an alternative and leaves the possibility to add DI containers later.

    In fact, quite a lot of the topics in your previous questions have been solved in Zend Framework already, e.g. Config classes. I am not saying use ZF, but you could check it out to see how they do things. Of course, you can look at the other frameworks too.

    Some pattern sites with PHP examples:

    • http://sourcemaking.com/creational_patterns
    • http://www.fluffycat.com/PHP-Design-Patterns/
    • http://www.ibm.com/developerworks/library/os-php-designptrns/?S_TACT=105AGX44&S_CMP=ART
    • http://www.ibm.com/developerworks/opensource/library/os-php-designpatterns/index.html?ca=drs-tp1308
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.