Need some help with inclusion of outside variables.
I have this file lang.class.php
class Lang {
public $DefaultLang = 'en';
public function __construct() {
require_once($_SERVER['DOCUMENT_ROOT'] . "/system/lang/en.php");
}
public function MainLang() {
return $this->DefaultLang;
}
public function Translate($String, $Array = "") {
if($Array != '')
$LangArray = $Array;
echo $LangArray[$String];
}
public function __destruct() {}
}
The language file is included in the __construct()
And this file en.php which holds an array
$LangArray = array(
"home" => "Home",
"news" => "News",
"info" => "Info"
);
The class is called by this: $Lang->Translate('home').
This returns nothing, but if I do this $Lang->Translate('home', $LangArray), it returns the value from the array_key home
So, how can I get the array without getting it via the function?
Thanks for your help!
When you use
require_*orinclude_*, think about it like if the code in the included file would simply be pasted instead of the include directive. Therefore, if you include that file, you will have a variable called$LangArray. You can take that variable and assign it to a private property in the class, then use it.