所以我是oo php的新手,我正在构建一个用于学习purpses的示例应用程序,我必须做的一件事是根据某些设置加载语言文件正如您将在下面发现的那样,代码分为两个类,一个设置类女巫应该加载语言文件,而另一个类在这种情况下"联系"女巫应该读取语言文件中的数组并显示属性消息。这是设置类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",
);
我查过谷歌,并尝试了一些其他东西,似乎无法让它工作,这可能是因为我不连贯地接近这个问题。请帮忙。
'
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);
}
?>`
self::$load = require 'en.php';
您在此处将文件包含的返回值分配给self::$load
,而不是您在语言文件中定义的变量$mess
。
所以,现在有两种方法:从语言文件中返回数组$mess
(return $mess;
)。
或者你可以写:
require 'en.php';
self::$load = $mess;
小旁注:我会if (self::$load)
检查您的Settings::getLanguage()
方法并返回 self::$load
变量,而不是重新获取语言文件......