doddsey_65 Posted July 8, 2011 Share Posted July 8, 2011 i have a language class like so: class languageClass { public static $lang; public static function setLang($lang) { self::$lang = $lang; } public static function getLang() { include(dirname(dirname(__FILE__)).'/languages/'.self::$lang.'.php'); return $lang; } public function loadFile($file) { $include = dirname(dirname(__FILE__)).'/languages/'.self::$lang.'/'.$file.'_lang.php'; if(file_exists($include)) { include $include; } else { echo 'File does not exist'; } } } so i include a language file within index.php like this: $lang->loadFile('common'); common_lang.php has an array like so: $l = array( 'HELLO' => 'hello', ); but when i try to echo $l['HELLO'] in the index.php file it says variable $l is undefined. But the language script includes the file where it is defined. If i include the file within index.php then everything works however Quote Link to comment https://forums.phpfreaks.com/topic/241407-language-class/ Share on other sites More sharing options...
trq Posted July 8, 2011 Share Posted July 8, 2011 Files included within functions (or class methods) only exist within that functions scope. Quote Link to comment https://forums.phpfreaks.com/topic/241407-language-class/#findComment-1240025 Share on other sites More sharing options...
doddsey_65 Posted July 8, 2011 Author Share Posted July 8, 2011 i thought so so i did this instead: public function loadFile($file) { $include = dirname(dirname(__FILE__)).'/languages/'.self::$lang.'/'.$file.'_lang.php'; if(file_exists($include)) { include $include; //var_dump($l); return $l; } else { echo 'File does not exist'; return false; } } but still get the same error Quote Link to comment https://forums.phpfreaks.com/topic/241407-language-class/#findComment-1240027 Share on other sites More sharing options...
trq Posted July 8, 2011 Share Posted July 8, 2011 Are you saving the return value of this loadFile() function? Quote Link to comment https://forums.phpfreaks.com/topic/241407-language-class/#findComment-1240028 Share on other sites More sharing options...
kisleki Posted July 8, 2011 Share Posted July 8, 2011 Hi for Everyone! I am totally new at PHP programming... I think uve heard it a lot... So my question: I have got a nrmal HTML frameset with 3 frames. One for logo ( has to be changed ) One for all contents ( has to be changed, consists 2 frames [menu and the readable content ]) One footer frame what should consists one link to change the site English and when its on English change it to Hungarian Ive tried to make it but that wasnt succesfull. for me the basic things are missing and i have still syntax problems. So where should i take the code about changing function? Is that function or session and what is the main logic it should works? My idea: take the main function about changing to the main( index.php ) and call it from footer with a link ( or button ) and i think the variables should store at a config.php file. But where to take the codes and how can someone write it to me? with a small explaination? I want to understand how does it work. I want to write it by myself after. Thanks a lot for help: Peter Quote Link to comment https://forums.phpfreaks.com/topic/241407-language-class/#findComment-1240192 Share on other sites More sharing options...
DavidAM Posted July 8, 2011 Share Posted July 8, 2011 @kisleki - You need to start your own thread for a new question. @doddsey_65 When you include a file within a function, any variables defined in that file are defined within the scope of the function. So, the $l variable only exists within the scope of the LoadFile method. Outside of that method, it is undefined. If you return this variable, then you need to capture the return value: $langMap = $lang->loadFile('common'); Although, in keeping with the encapsulation concept of OOP, I think I would assign that variable to a private property within the class, and define another method to translate. Something like this: class languageClass { public static $lang; private $_map; // PROPERTY FOR LANGUAGE MAP public static function setLang($lang) { self::$lang = $lang; } public static function getLang() { include(dirname(dirname(__FILE__)).'/languages/'.self::$lang.'.php'); return $lang; } public function loadFile($file) { $include = dirname(dirname(__FILE__)).'/languages/'.self::$lang.'/'.$file.'_lang.php'; if(file_exists($include)) { include $include; $this->_map = $l; // STORE THE MAP IN A PRIVATE PROPERTY return $l; // OR RETURN THE MAP TO THE CALLER (OR BOTH) } else { echo 'File does not exist'; } } // METHOD TO TRANSLATE A WORD public function translate($word) { return $this->_map[$word]; } } Of course, I just copied your code and added a couple of things. I don't know why you are using STATIC for parts of it and not for others. You probably need to reconsider the design. Also, you'll need to add error checking (what if $word does not exist in the map?). Quote Link to comment https://forums.phpfreaks.com/topic/241407-language-class/#findComment-1240202 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.