0

所以我是oo php的新手,我正在构建一个用于学习purpses的示例应用程序,我必须做的一件事是根据一些设置加载一个语言文件正如您将在下面发现的那样,代码分为两个类,一个设置类女巫应该加载语言文件和另一个类在这种情况下“联系”女巫应该读取语言文件中的数组并显示propper消息。这是设置类 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;
   }

}

第二课:

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不是您在语言文件中定义的变量。

所以,现在有两种方法:从语言文件中返回数组$mess( )。return $mess;

或者你可以写:

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

小旁注:我会检查您的Settings::getLanguage()方法if (self::$load)并返回self::$load变量而不是重新获取语言文件......)

于 2013-10-07T15:00:16.667 回答