Php访问类上的静态方法

时间:2021-10-11 04:07:17

First all i'm new on OOP. Sorry if this is a noob question.

首先,我是OOP的新手。对不起,如果这是一个菜鸟问题。

I'm building a multilanguage web site. I'm fetching database values with languga iso's. For eg: index.php?lang=en if language value is set with $_GET parameter, mysql brings only en values in database. The problem is with my LanguageController.

我正在建立一个多语言网站。我正在使用languga iso获取数据库值。例如:index.php?lang = zh_如果使用$ _GET参数设置语言值,则mysql仅在数据库中引入en值。问题出在我的LanguageController上。

// LanguageController Class
class LanguageController {
    public $lang = "en";
    public function __construct() {
        return true;    
    }
    public static function detectLang() {
        $lang = 'en';
        ob_start();
        session_start();
        if(isset($_GET["lang"])) {
            $lang= $_GET["lang"];
            $_SESSION["lang"] = $lang;
            setcookie("lang", $lang, time() + (3600 * 24 * 30));
        }
        else {
            $lang = 'en';
            $_SESSION["lang"] = $lang;
            setcookie("lang", $lang, time() + (3600 * 24 * 30));
            return true;
        }
    }
}

I want to pass default $lang parameter for eg 'en'. If client change language to 'fr', i must start a session. And mysql brings 'fr' language values. How can i pass my $lang parameter on my detectLang() method.

我想传递默认的$ lang参数,例如'en'。如果客户将语言更改为'fr',我必须开始会话。而mysql带来'fr'语言值。如何在detectLang()方法上传递$ lang参数。

I tried var_dump(LanguageController::detectLang()) It's coming NULL.

我试过var_dump(LanguageController :: detectLang())它来了NULL。

Any help greatly appricated.

任何帮助都很有用。

2 个解决方案

#1


1  

When there's no return statement in a function - returned value is NULL. Obviously your detectLang function works and the isset($_GET["lang"]) condition is true, in this case the returned value is NULL.

当函数中没有return语句时 - 返回值为NULL。显然你的detectLang函数工作,并且isset($ _ GET [“lang”])条件为true,在这种情况下返回的值为NULL。

#2


-1  

In static methods you can only reference to static attributes. In simple words, the $lang needs to be static as well i.e.

在静态方法中,您只能引用静态属性。简单来说,$ lang也需要是静态的,即

public static var $lang = "en";

#1


1  

When there's no return statement in a function - returned value is NULL. Obviously your detectLang function works and the isset($_GET["lang"]) condition is true, in this case the returned value is NULL.

当函数中没有return语句时 - 返回值为NULL。显然你的detectLang函数工作,并且isset($ _ GET [“lang”])条件为true,在这种情况下返回的值为NULL。

#2


-1  

In static methods you can only reference to static attributes. In simple words, the $lang needs to be static as well i.e.

在静态方法中,您只能引用静态属性。简单来说,$ lang也需要是静态的,即

public static var $lang = "en";