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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:29:29+00:00 2026-05-26T05:29:29+00:00

Possible Duplicate: Loading multiple versions of the same class What is the best way

  • 0

Possible Duplicate:
Loading multiple versions of the same class

What is the best way to setup different versions of PHP classes and toggle them based on a configuration value?

Here’s my scenario (using Zend Framework – shouldn’t matter, but might):

I have 2 different versions of a web service that I’d like to be able to call. I need to switch between them using a value in my config file.

Right now, I have a class that acts as a factory and determines what version of the class needs to be returned based on the config value. Both versions of the class that the factory returns extend the same abstract class, so once the factory returns the object, I can treat both versions the same in the rest of the code. This seems fine to me in theory, but I end up with code that looks like this in my factory class, which doesn’t seem to be good practice:

require_once APPLICATION_PATH . '/models/Search/SearchModelV' . $this->config->model->version . '.php';

$model_class = 'Search_Model_V' . $this->config->model->version;
return new $model_class();

I’m using PHP 5.3, so I’ve implemented namespaces. Just throwing that out there in case it can be used as a 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-26T05:29:30+00:00Added an answer on May 26, 2026 at 5:29 am

    I think the solution is probably the “factory” pattern. This means that you have a static function that makes an appropriate object based on a parameter. This is possible even on an abstract class, because you can declare and call static methods on abstract classes.

    abstract class Search_Model_Abstract {
        public static function create($version) {
            $class = 'Search_Model_V' . $version;
    
            return new $class;
        }
    }
    

    You can then create the appropriate object with Search_Model_Abstract::create($this->config->model->version)

    Note that this method really should also handle any exceptions that might be thrown from the constructors, and it should also check $version against possible values.

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

Sidebar

Related Questions

Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Dynamically loading a class in Java hi i am trying to create
Possible Duplicate: Loading DLLs into a separate AppDomain What is the proper way to
Possible Duplicate: Problem when loading php file into variable (Load result of php code
Possible Duplicate: Javascript callback when IFRAME is finished loading? Is there any way to
Possible Duplicate: C/C++ Image Loading I need a C\C++ cross-platform library for loading/displaying images(jpg,gif,png,tga).
Possible Duplicate: Css and javascript Not Loading in rewrite rule After using only rewrite
Possible Duplicate: Can I use predefined namespaces when loading an XDocument? I have following
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Dynamic Resource Loading Android In Android, I can load a string from

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.