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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:46:00+00:00 2026-05-20T11:46:00+00:00

I know about the dependency injection pattern which seems like the best solution available

  • 0

I know about the dependency injection pattern which seems like the best solution available to handle classes that require instances of other classes to function properly. But when it comes to handling the scenario of a certain class existing, but in a wrong version, DI or any other OOP pattern obviously isn’t going to help at all.

What is a good way to handle version-based dependencies of libraries in PHP?

Here’s an illustrated way to show what I mean (psuedo-code):

class payment_module_base {

    var $baseVersion;

    function __construct() {
        $this->baseversion = 12; //Version 12
    }

    // Other relevant methods...
}

class card_payments extend payment_module_base {

    function construct() {
        if ($this->baseversion <= 10)
        Throw New Exception("Card payments isnt working with base module V. <10");
    }

}

Note: This is for illustrative purposes – I know that this should be lifted out to specific tests to not clutter the production code with conditionals regarding versions unless really neccessary.

  • 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-20T11:46:01+00:00Added an answer on May 20, 2026 at 11:46 am

    A lot of people swear by definitions in the top of library files. It’s simple, and the performance is good … perhaps too simple.

    // -- At the top of the library, we define this ...
    define('PAYMENT_LIBRARY_VER', 324);
    
    // -- At some point in the code, we check it!
    if( defined('PAYMENT_LIBRARY_VER') && PAYMENT_LIBRARY_VER <= 320 )
        throw new HissyFit("This is just an alias for an exception.");
    

    I think it’s a bit unwieldy myself, and prefer factory version management along the lines of …

    // -- In a file far far away ...
    class PaymentProcessor_r324 extends PaymentProcessor
    {
        ...
    }
    
    // -- Somewhere else ...
    class PaymentProcessor_r325 extends PaymentProcessor
    {
        ...
    }
    
    // -- In the main library class "holder"
    class PaymentProcessorManager
    {
        /**
         * The latest version of the library
         */
        const LATEST_VERSION = 324;
    
        /**
         * Initialise a new instance of the PaymentProcessor.
         * @param $ver
         * @return PaymentProcessor
         */
        public static function Initialise( $revision = null )
        {
            if( $revision === null )
                $revision = self::LATEST_VERSION;
    
            var $c = "PaymentProcessor_r{$revision}";
    
            return new $c();
        }
    }
    
    // -- Pretty much anywhere you like, and autoloading friendly to boot.
    if( PaymentProcessorManager::LATEST_VERSION < 300 )
        throw new HissyFit("This is just an alias for an exception.");
    

    This model is more extensible. You have to reference the class to get the latest version, but don’t need an instance of it. The reference means that any autoloading code you have will get a chance to pick up the class, and you can run version checks at any point in the pipeline. At the same time, you can optionally support multiple versions of the class, and easily load development versions.

    // -- Even though the 'latest version' is 324, there's nothing to stop me
    //    from loading my development version, #325.
    var $paymentProcessor = PaymentProcessorManager::Initialise(325);
    

    This model also supports building dependency headers …

    if( PaymentProcessorManager::LATEST_VERSION < 300
     || OtherModuleManager::LATEST_VERSION < 240 )
        throw new HissyFit("The minimum requirements for this functionality are not met.");
    

    I would say as a boot note though that PHP doesn’t seem to lend itself well to this kind of arrangement … it’s pretty loose, and requires a fair amount of manual checking to ensure global compatibility. If reasonably possible, I’d avoid versioning in the wonderful world of PHP.

    Update: Oh yes, and I almost forgot. You’d very possibly want a class alias along the lines of the following to not require horrible version specific chains.

    class_alias('PaymentProcessor_r' . PaymentProcessorManager::LATEST_VERSION, 'PaymentProcessorLatest');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know a good online resource about using SQL Server 2005 dependency cache
I know about the HIG (which is quite handy!), but what programming practices do
I know about SortedSet , but in my case I need something that implements
I know about this question: Which (third-party) debug visualizers for Visual Studio 2005/2008 do
I know about code-behind files, but what is the best real-world way of Designers
I'm trying to introduce dependency injection (DI) as a pattern here at work and
I've had a certain feeling these last couple of days that dependency-injection should really
There are already quite some posts about the Singleton-Pattern around, but I would like
I was reading an exellent article about how to use dependency injection with WCF
This is a question about dependency injection. When constructing a service object, we pass

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.