Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i correctly assign a value to a private static variable in PHP class?
    primarykey
    data
    text
    <p>I have a class that translate language array base. So the problem is that the language does not change base on a cookie value. </p> <p>this function should set the language value but it does not. it seems that no matter what i do i always get "ar" as a self::$currlang value. how can i correct this issue?</p> <pre><code>public function _set(){ if( $_COOKIE['defaultLang'] != '' ) { self::$currlang = $_COOKIE['defaultLang']; } else { //this is the default language self::$currlang = 'ar'; } } </code></pre> <p>here is my code</p> <p>thanks for your help :)</p> <pre><code>&lt;?php include('../langs/english.php'); include('../langs/arabic.php'); class Translator{ private static $strs = array(); private static $currlang = ""; public function _set(){ if( $_COOKIE['defaultLang'] != '' ) { self::$currlang = $_COOKIE['defaultLang']; } else { //this is the default language self::$currlang = 'ar'; } } public static function loadTranslation($lang, $strs){ if (empty(self::$strs[$lang])) self::$strs[$lang] = array(); self::$strs[$lang] = array_merge(self::$strs[$lang], $strs); } public static function setDefaultLang($lang){ self::$currlang = $lang; } public static function getDefaultLang(){ return self::$currlang; } public static function translate($key, $lang=""){ if ($lang == ""){ $lang = self::$currlang; } $str = self::$strs[$lang][$key]; if (empty($str)){ //$str = "$lang.$key"; $str = 'Language "'. $lang . '", '. $key . ' is not defined.'; } return $str; } public static function freeUnused(){ foreach(self::$strs as $lang =&gt; $data){ if ($lang != self::$currlang){ $lstr = self::$strs[$lang]['langname']; self::$strs[$lang] = array(); self::$strs[$lang]['langname'] = $lstr; } } } public static function getLangList(){ $list = array(); foreach(self::$strs as $lang =&gt; $data){ $h['name'] = $lang; $h['desc'] = self::$strs[$lang]['langname']; $h['current'] = $lang == self::$currlang; $list[] = $h; } return $list; } public static function &amp;getAllStrings($lang){ return self::$strs[$lang]; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload