dimanche 11 octobre 2015

Alternative logic to PHP class aliases with conditions

In my application I can have different rules in different cultural contexts. I have the following code.

<?php

class Rules_IT {

  const MYCONST_1 = 5;
  const MYCONST_2 = 3;
  const MYCONST_3 = 10;

  static public function myMethod(){
      return 'Something';
  }
}

class Rules_DE {

  const MYCONST_1 = 3;
  const MYCONST_2 = 2;
  const MYCONST_3 = 15;

  static public function myMethod(){
      return 'Something different';
  }
}


if($pattern == 'IT'){
  class_alias('Rules_'.$pattern, 'Rules');
}
else if($pattern == 'DE'){
  class_alias('Rules_'.$pattern, 'Rules');
}
else {
  // [...]
}

// And after I use the class Rules ... 
print Rules::myMethod();

It works. My IDE (NetBeans 7.x) don't know the class Rules and I can't use the code autocomplete, but isn't a big problem.

But there are some alternatives? Some different approach in the logic? Some design-pattern to handle the problem?

Thank you.

Aucun commentaire:

Enregistrer un commentaire