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

  • Home
  • SEARCH
  • 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 704105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:54:16+00:00 2026-05-14T03:54:16+00:00

How would one typically create a dynamic factory method in PHP? By dynamic factory

  • 0

How would one typically create a dynamic factory method in PHP? By dynamic factory method, I mean a factory method that will autodiscover what objects there are to create, based on some aspect of the given argument. Preferably without registering them first with the factory either. I’m OK with having the possible objects be placed in one common place (a directory) though.

I want to avoid your typical switch statement in the factory method, such as this:

public static function factory( $someObject )
{
    $className = get_class( $someObject );
    switch( $className )
    {
        case 'Foo':
            return new FooRelatedObject();
            break;
        case 'Bar':
            return new BarRelatedObject();
            break;
        // etc...
    }
}

My specific case deals with the factory creating a voting repository based on the item to vote for. The items all implement a Voteable interface. Something like this:

Default_User implements Voteable ...
Default_Comment implements Voteable ...
Default_Event implements Voteable ...

Default_VoteRepositoryFactory
{
    public static function factory( Voteable $item )
    {
        // autodiscover what type of repository this item needs
        // for instance, Default_User needs a Default_VoteRepository_User
        // etc...
        return new Default_VoteRepository_OfSomeType();
    }
}

I want to be able to drop in new Voteable items and Vote repositories for these items, without touching the implementation of the factory.

  • 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-14T03:54:16+00:00Added an answer on May 14, 2026 at 3:54 am

    If the switch statement is out you’re basically down to naming conventions. Take the class of the passed in object and use it to instantiate the new class. Simple example below.

    //pseudo code, untested
    class Default_VoteRepositoryFactory
    {
        public static function factory( Voteable $item, $arg1, $arg2 )
        {
            $parts = explode('_', get_class($item));
            $type  = array_pop();
            $class = 'Default_VoteRepository_' . $type;
            return new $class($arg1, $arg2);
            // autodiscover what type of repository this item needs
            // for instance, Default_User needs a Default_VoteRepository_User
            // etc...
        }
        //can be as complex or as simple as you need
        protected static function getType(stdClass $item)
        {
            $parts = explode('_', get_class($item));
            return array_pop($parts);       
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would one typically implement a service layer in an MVC architecture? Is it
I need to create a application that will allow a Windows PC (XP/Vista) to
How would one shorten the following using ternary operators? if ((pos - maxPos) ==
How would one insert an array full of smtp email addresses into an AD
How would one enumerate the installed browsers on an OS X system from a
How would one switch a public bool to true from a child form in
How would one follow the proper steps in order to license a package under
Why would one use realloc() function to resize an dynamically allocated array rather than
Why would one use func( const Class &value ) rather than just func( Class
How would one go about either using the 3D components of WPF or using

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.