Yuri Nko Yuri Nko - 3 months ago 10
PHP Question

PHP class variable loads unproperly

i18n Object
(
[localization] =>
[timezone] =>
[lang] =>
[english] => english
[strings] => Array
(
[main/welcome] => welcome
)

)


I get this from

class i18n {
public $localization;
public $timezone;
public $lang;
private static $instance; //The single instance
public static function getInstance(){
if (self::$instance == null){
self::$instance = new self($localization);
}
return self::$instance ;
}
public function __construct($localization) {
$this->$localization = $localization;
// if($this->localization == 'english') { // ill be writing these sick things like idiot for each translation, FML
$this->strings = english::load();
//}
// TODO $this->strings = $localization::load();
}
public function getString($string_id) {
return $this->strings[$string_id];
}
}
?>


I dont get how it happens, can somebody please explain me what happened?
I think it should work different, like having english in the localization for example... I would like to know how it happened, in advance have some way to fix it as I am struggling trought my own CMS or something, I was just bored so I code, but I dont manage how to get this issue done...

It must be something inside php... does php support internalization? Does it have internal system for that? I would like to know, I dont want invent whole wheel again, just part of it. Thank you very much...

I really wonder what it does and thank you very much for your response.

Answer

I guess you do not have all error_reporting on. Please make sure to do that, then you should see something like Notice: Undefined variable: $localization from your code. If you update your code with the below, you should see english in the localization.

public static function getInstance($localization = 'english'){
    if (self::$instance == null){
        self::$instance = new self($localization);
    }
    return self::$instance ;
 }

public function __construct($localization) {
    $this->localization = $localization;
    // if($this->localization == 'english') { // ill be writing these sick things like idiot for each translation, FML
        $this->strings = english::load();
    //}
    // TODO $this->strings = $localization::load();
}
public function getString($string_id) {
    return $this->strings[$string_id];
}

Then instantiate your class as,

$object = i18n::getInstance('french'); // for languages other than english

So, basically I changed two things

  1. I added an argument $localization with your getInstance method.
  2. I changed the line $this->$localization = $localization; to $this->localization = $localization; in your constructor.

Please observe there is a difference between $this->$localization = $localization; and $this->localization = $localization;. $this->$localization is a variable variable. Meaning once you are passing lets say english as an argument to your constructor it makes a variable called $english and assigns value in it. Thats why you are seeing the english = english thing