So i am new to oo php and i-m building a sample app for learning purpses , one thing i must do is load a language file according to some settings The code, as you will discover below is divided between two classes , a settings class witch should load the language file and another class in this case "contact" witch should read the array in the language files and display the propper message. this is the Settings class the lang variable sets the default language it can take 2 values at the moment : ro- for romanian and en - for english ,
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;
}
}
The second class :
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'];
}
}
}
A sample from the language file:
$mess = array ("name_error" => "You must insert your name",
"file_written" => "the file has been written",
);
I have looked up on google , and tried some other stuff and can't seem to get it to work , and that may be because i am approaching this problem incorectly. Plese help.
`
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';
You assign here a return value from the file inclusion to self::$load
; not the variable $mess
you defined in your language file.
So, there are two ways now: return the array $mess
(return $mess;
) from the language file.
Or you can just write:
require 'en.php';
self::$load = $mess;
Little side note: I'd check in your Settings::getLanguage()
method if (self::$load)
and return then the self::$load
variable instead of refetching the language file…)