0

だから私は oo php を初めて使い、目的を学習するためのサンプルアプリを構築しています。私がしなければならないことの 1 つは、いくつかの設定に従って言語ファイルをロードすることです。以下でわかるように、コードは 2 つのクラスに分かれています。設定クラスの魔女は言語ファイルと別のクラスをロードします。この場合、「contact」ウィッチは言語ファイルの配列を読み取り、適切なメッセージを表示する必要があります。これは設定クラスです。 lang 変数は、現時点で 2 つの値を取ることができるデフォルト言語を設定します: ro- ルーマニア語と en - 英語、

    class Settings
{
  static public $lang = 'ro';
  static public $load;

  static public function get_language()
   {
    if(self::$lang == 'ro')
    {
      self::$load = require 'ro.php';
    }
    elseif(self::$lang == 'en')
    {
     self::$load = require 'en.php';
    }
    return self::$load;
   }

}

2 番目のクラス :

class Contact extends Settings {
   //proprietati

   public $nume;
   public $subiect;
   public $mesaj;
   public $dincs;


   //comportament - metode

   public function __construct()
   {
     //$this->dincs = 'Din construct';
     parent::get_language();
   }


   public function write_file()
   {

     if(empty($this->nume))
     {
       return $mess['name_error'];
     }
     else
     {
       $fp = fopen('data.txt', 'w');
       fwrite($fp, $this->nume.".".$this->subiect .".". $this->mesaj."|".$this->dincs ."|".parent::$load);
       fclose($fp);

       return $mess['file_written'];
     }
   }

}

言語ファイルのサンプル:

   $mess = array ("name_error"  => "You must insert your name",
                  "file_written" => "the file has been written",

   );

私はグーグルで調べて、他のことを試してみましたが、うまくいかないようです。これは、この問題に間違って取り組んでいる可能性があります。助けてください。

4

2 に答える 2

1

`

class Settings
{
    protected $language = 'ro';
    protected $load;

    public function setLanguage($language = 'ro')
    {
        $this->language = $language;

        // file_get_contents()
        $this->load = require($this->language . '.php');
    }

    public function getLanguage()
    {
        return $this->language;
    }

}

class Contact extends Settings 
{
   protected $name;
   protected $subject;
   protected $message;
   protected $dincs;
   protected $settingsObject;

   // set all the properties below...

   public function setName($name)
   {
        $this->name = $name;
   }   

   public function setSubject($subject)
   {
        $this->subject = $subject;
   }   

   public function setMessage($message)
   {
        $this->message = $message;
   }   

   public function setDincs($dincs)
   {
        $this->dincs = $dincs;
   }

   // get all the properties...
   public function getName()
   {
        return $this->name;
   }

   // This function only accepts an instance of Settings as the parameter
   public function setSettingsObject(Settings $settings)
   {    
        $this->settingsObject = $settings;
   }

   public function writeContentsToFile()
   {
        // if there is nothing in name, throw a new exception
        if (empty($this->name))
        {
            throw new Exception('name has not been set! Set name before calling '. __METHOD__);
        }
        else
        {
           // get the file
           $fp = fopen('data.txt', 'w');

           // formatted string
           $contents = sprintf('%s . %s . %s | %s | %s', $this->name, $this->subject, $this->message, $this->dincs, $this->settingsObject->getLanguage());

           // write to the file
           fwrite($fp, $contents);

           // close the handler
           fclose($fp);

           return ('File written! Contents written: ' . $contents);
        }
    }
}


// instantiate settings
$settings = new Settings();
$settings->setLanguage('en');

// instantiate contact and set the settings object
$contact = new Contact();
$contact->setName('Joe Smith'); // if this is not set, ::writeContentsToFile will throw an Exception
$contact->setSettingsObject($settings);

// try and catch the Exception that ::writeContentsToFile may throw
try 
{
    echo $contact->writeContentsToFile();
}
catch (Exception $exception)
{
    var_dump($exception);
}

?> `

于 2013-10-07T17:49:33.767 に答える
0
self::$load = require 'en.php';

ここで、ファイル インクルードからの戻り値を に割り当てますself::$load$mess言語ファイルで定義した変数ではありません。

したがって、2 つの方法があります。言語ファイルから配列$mess( ) を返します。return $mess;

または、次のように書くこともできます。

require 'en.php';
self::$load = $mess;

ちょっとした補足: 言語ファイルを再取得する代わりに、Settings::getLanguage()メソッドをチェックインしてから変数if (self::$load)を返します…)self::$load

于 2013-10-07T15:00:16.667 に答える