开发者

PHP OO class for setting up vocabulary list

I w开发者_开发百科ant to create a class to store a small list of multi-lingual vocabulary used in many places, I want to replace a large number of 'include' statements that are currently used in program. Here is what I was thinking of (it is not fully functional). Can you help me make work and use proper OO constructs.

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();

?>


in your main function, try this...

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


Try this: you need to always reference values and methods inside a class with $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'];
 }
}

But to got further, why not make it less array-ish:

$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;
    }
}


$this is in reference to the current object (from within an object). Since you have the $vocab property inside the object, you can say $this->vocab to access that property. You can also do so without the $this, but $this (IMHO) makes things more clear regarding which objects you're referencing as you begin to inherit the objects.

Also, if these are hard-coded values and not pulled from a database, why not use a language file with defines in it? Such as:

languages/english.php

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

languages/french.php

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

Then include the correct file in your "common.php" or "includes.php" file. From there you can use the constants throughout your page. You could even extend it by making (if this is the case) the english file the default and perform

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

Then you can load the other language first then english.php on top (So you can make sure you have at least a default value should one not be translated yet)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜