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');