views:

62

answers:

3

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 = phpversion();
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?

+2  A: 

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.

TravisO
+1  A: 

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.

chelmertz
It would work the same way if I didn't rely on the `private` and `public` visibility keywords since those aren't PHP4 compatible, right?
meder
It would if you only used the method and not the property (since afaik it's always public the php4's object model.) Good catch by you!
chelmertz
+2  A: 

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 :)

gnud
If the prerequisite is PHP4, I don't think your method would fail gracefully if it tries to use Zend_Translate and fails on some of it's methods, since the class probably (haven't tested) can be defined in PHP4.
chelmertz
The class can't be defined if it uses php5 keywords like `private` or `public`. The version of Zend_Translate I had handy, does.
gnud