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?
method_exists
(method_exists ( mixed $object , string $method_name )
) function, which is less restrictive and more reliable.