Loading a language file in oo php -
so new oo php , i-m building sample app learning purpses , 1 thing must load language file according settings code, discover below divided between 2 classes , settings class witch should load language file , class in case "contact" witch should read array in language files , display propper message. settings class lang variable sets default language can take 2 values @ moment : ro- romanian , en - 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 language file:
$mess = array ("name_error" => "you must insert name", "file_written" => "the file has been written", ); i have looked on google , , tried other stuff , can't seem work , , may because approaching 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 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; } // properties... public function getname() { return $this->name; } // function accepts instance of settings parameter public function setsettingsobject(settings $settings) { $this->settingsobject = $settings; } public function writecontentstofile() { // if there nothing in name, throw new exception if (empty($this->name)) { throw new exception('name has not been set! set name before calling '. __method__); } else { // 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 file fwrite($fp, $contents); // close handler fclose($fp); return ('file written! contents written: ' . $contents); } } } // instantiate settings $settings = new settings(); $settings->setlanguage('en'); // instantiate contact , set settings object $contact = new contact(); $contact->setname('joe smith'); // if not set, ::writecontentstofile throw exception $contact->setsettingsobject($settings); // try , catch exception ::writecontentstofile may throw try { echo $contact->writecontentstofile(); } catch (exception $exception) { var_dump($exception); } ?> `
Comments
Post a Comment