8

When creating an extension for commercial use it would be nice if it works on multiple Magento versions, maybe CE and EE also.
But sometimes this doesn't work as planned because the core changes from version to version.
My question is about, how should I handle the small core changes that affect my extension. I'm not talking about huge changes that require a re-factoring.
Here is an example to make it clearer.
Starting CE 1.8.1 (and EE 1.13.1) the method Varien_Io_File::write changed the way it works (the signature is the same) and the method filePutContent appeared that does something similar to what write used to do previously.
Right now I do something hacky and check the version of Magento in order to know what method to use to achieve the same result

/** @var Varien_Io_File $io */
if (version_compare(Mage::getVersion(), '1.8.1', '<')) {
    $io->write($destinationFile, $contents, 0777);
} else {
    $io->filePutContent($destinationFile, $contents);
}

Now, it I want this to work on EE also It gets even more ugly because 1.8.1 is specific to CE. So the code above becomes

/** @var Varien_Io_File $io */
if (version_compare(Mage::getVersion(), $this->getCurrentVersion(), '<')) {
    $io->write($destinationFile, $contents, 0777);
} else {
    $io->filePutContent($destinationFile, $contents);
}

where the method getCurrentVersion looks like this

public function getCurrentVersion()
{
    if (Mage::getEdition() == Mage::EDITION_ENTERPRISE) {
        return '1.13.1';
    }
    return '1.8.1';
}

The code above is a bit simplified (I use proper constants for versions in my real code)

But this works for CE 1.7 and above when the method Mage::getEdition() was introduced. If I want it to work on version prior to ce 1.7 it gets even crazier.

Now imagine this changes again in version CE 1.11 and EE 1.16. Chaos is bound to happen.

Is there a cleaner way of doing this without doing if or case statements?

  • I haven't been confronted to that problem, but maybe my opinion can be helpful. For readability purposes, I would probably group all those "problematic" methods in the same file, like a helper. Also, for the example you gave above, I would also use the method_exists (method_exists ( mixed $object , string $method_name )) function, which is less restrictive and more reliable. – Mayers May 20 '15 at 13:56
  • @Mayers. You should write this as an answer. Looks like a valid approach. Not a perfect one, but a valid one indeed :) – Marius May 20 '15 at 13:58
3

I haven't been confronted to that problem, but maybe my opinion can be helpful. For readability purposes, I would probably group all those "problematic" methods in the same file, like a helper. Also, for the example you gave above, I would also use the method_exists function (http://php.net/manual/en/function.method-exists.php), which is less restrictive and more reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.