开发者

Is it a good idea and reliable to branch out on the php version number?

I'm working on some new components in my framework for a gettext implementation. It needs to support the dead php4 and php5+. I'm using Zend_Translate and I will implement a gettext based class for php4.

I'm wondering if it's ok to rely on the phpversion function to branch out on which class to implement. Something like...

$version = phpver开发者_运维技巧sion();
define('VERSION', grabVersion($version) ); // grab major version

if ( VERSION >= 5 ) {
   $local = new Zend_Translate();
} else {
   $local = new Gettext();
}

Both classes have a _ function to do the translations, so the rest should be the same.

<label for="first_name"><?php echo $local->_("First Name");?></label>

Is it common to do so in any of the major php frameworks, would you do something similar if you were forced to support the dead PHP4?


If I was going to implement this across PHP versions, I would rely on the version number too. So what you're proposing seems perfectly sane and acceptable.


It's not a terrible idea to branch on a php version number, but I would prefer something like this:

if(class_exists("Zend_Translate")) {
   $local = new Zend_Translate();
} else if(class_exists("Gettext")) {
   $local = new Gettext();
} else {
   throw new Exception("No supported translation helper found");
}

If you wanted to, you could make it more dynamic:

$translators = array(
    "Zend_Translate",
    "Gettext",
    "FooTranslate",
);
$local = null;

foreach($translators as $t) {
    if(class_exists($t)) {
        $local = new $t();
        break;
    }
}

if($local === null) {
    throw new Exception("No supported translation helper found");
}

A better place to branch based on version, is error handling - above I use exceptions, which are not available in PHP4 :)


I think getting the version number is a good idea, but it should be implicit so it's not called out of the blue.

$local = TranslateFactory::create();
$local->_("translate me");

class TranslateFactory {
    private static $_translator;    
    private static function _getTranslator() {
        if(empty(self::$_translator)) {
            // get php version
            // if-case here
            self::$_translator = // result your if-case
        }
        return self::$_translator;
    }
    public static function create() {
        return _getTranslator();
    }
}

That provides a per-request cache at least. The _getTranslator() could even fetch object from a longer lasting cache if you'd need it, just by putting the logic in that one spot fetching your real object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜