【问题标题】:PHP OO class for setting up vocabulary list用于设置词汇表的 PHP OO 类
【发布时间】:2023-03-15 05:11:02
【问题描述】:

我想创建一个类来存储在许多地方使用的多语言词汇的小列表,我想替换程序中当前使用的大量“包含”语句。这就是我的想法(它不是完全可用的)。你能帮我完成工作并使用正确的 OO 结构吗?

class VocabClass{ 

  public $term = array();    

  public function __construct(){ 
   // my vocabulary... 
   $this->term['hello']['E'] = 'hello';  
   $this->term['hello']['F'] = 'bonjour';  
   $this->term['goodbye']['E'] = 'goodbye';  
   $this->term['goodbye']['F'] = 'aurevoir';       
  }
}

class Program{

 public $vocab;
 function __construct(){ 
   $vocab = new VocabClass();       
   // this works
   echo $vocab->term['hello']['F']; 
 }

 function Main() {

  // this doesn't work
  echo $vocab->term['hello']['E'];   
  echo $this->term['hello']['E'];   


 }
}


$myProgram = new Program();
$myProgram->Main();

?>

【问题讨论】:

    标签: php oop constructor scope


    【解决方案1】:

    $this 引用当前对象(来自对象内部)。因为你在对象中有 $vocab 属性,你可以说 $this->vocab 来访问那个属性。您也可以在没有 $this 的情况下这样做,但是 $this (恕我直言) 让您在开始继承对象时更清楚地了解您正在引用哪些对象。

    另外,如果这些是硬编码的值并且不是从数据库中提取的,为什么不使用包含定义的语言文件呢?如:

    语言/english.php

    define('LANG_YES', 'Yes');
    define('LANG_NO', 'No');
    define('LANG_CANCEL', 'Cancel');
    define('LANG_WELCOME', 'Welcome!');
    

    语言/法语.php

    define('LANG_YES', 'Oui');
    define('LANG_NO', 'N');
    define('LANG_CANCEL', 'Annuler');
    define('LANG_WELCOME', 'Bienvenue');
    

    然后在“common.php”或“includes.php”文件中包含正确的文件。从那里您可以在整个页面中使用常量。您甚至可以通过将英文文件设为(如果是这种情况)默认文件并执行来扩展它

    if (!defined('LANG_YES')) define('LANG_YES','Yes');
    

    然后您可以先加载另一种语言,然后在顶部加载 english.php(这样您就可以确保至少有一个默认值应该尚未翻译)

    【讨论】:

      【解决方案2】:

      试试这个:你需要始终使用 $this->

      引用类中的值和方法
      class VocabClass{
      
        public $term = array();
      
        public function __construct(){
         // my vocabulary...
         $this->term['hello']['E'] = 'hello';
         $this->term['hello']['F'] = 'bonjour';
         $this->term['goodbye']['E'] = 'goodbye';
         $this->term['goodbye']['F'] = 'aurevoir';
        }
      }
      
      class Program{
      
       public $vocab;
       function __construct(){
         $this->vocab = new VocabClass();
       }
      
       function Main() {
      
        // this doesn't work
        echo $this->vocab->term['hello']['E'];
       }
      }
      

      但是为了更进一步,为什么不减少它的数组式:

      $p = new Program();
      $p->Main();
      
      class VocabClass {
          const ENGLISH = 1;
          const FRENCH = 2;
      
          public $hello;
          public $goodbye;
      
          public function __construct($language) {
              switch ($language) {
                  case self::ENGLISH:
                      $this->hello = 'hello';
                      $this->goodbye = 'goodbye';
                      break;
                  case self::FRENCH:
                      $this->hello = 'bonjour';
                      $this->goodbye = 'aurevoir';
              }
          }
      }
      
      class Program {
          public $vocab;
      
          function __construct() {
              $this->vocab = new VocabClass(VocabClass::ENGLISH);
          }
      
          function Main() {
              echo $this->vocab->hello;
          }
      }
      

      【讨论】:

        【解决方案3】:

        在你的主函数中,试试这个...

        function Main(){
            echo $this->vocab->term['hello']['E'];
        }
        

        【讨论】:

          猜你喜欢
          • 2011-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多