如何在课堂上写一个if语句?

时间:2016-11-01 06:35:53

标签: php

我有一个这样的课程:

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    // I need to put a if() in here

    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    // there is some other methods in here

}

正如我在上面的代码中所评论的那样(// I need to put a if() in here),我需要比较其中的内容并将结果用作$redirectTo变量的值。

但PHP不允许我这样做。我的意思是,我想要这样的事情:

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    if ( Lang::getLocale() == 'en' ){
        $lang = '/en';
    } else {
        $lang = '/fa';
    }

    protected $redirectTo = '/home'.$lang;

    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    // there is some other methods in here

}

正如我所说,php不允许我这样做^。我怎么能这样做?

编辑:我不能将if语句放入__construct()方法。由于setLocale()尚未设置,getLocale()始终返回默认语言(即fa。无论如何,我需要将{if}语句放在__construct()之外。有什么建议吗?

3 个答案:

答案 0 :(得分:4)

你可以尝试这个:

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

protected $lang='';
protected $redirectTo='';


    public function __construct()
    {
    if ( Lang::getLocale() == 'en' ){
        $this->$lang = '/en';
    } else {
        $this->$lang = '/fa';
    }

    $this->$redirectTo= '/home'.$lang;

        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    // there is some other methods in here

}

答案 1 :(得分:3)

将您的逻辑添加到构造函数:

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo;

    public function __construct()
    {
        if ( Lang::getLocale() == 'en' ){
            $lang = '/en';
        } else {
            $lang = '/fa';
        }
        $this->redirectTo = '/home'.$lang;

        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    // there is some other methods in here

}

<强>更新

类代码只是类的定义。它不以任何方式执行。只有在显式调用它们时才能执行类方法。

不能除属性,常量或方法之外的任何内容。

如果您需要执行某些操作 - 创建类方法。 没有其他方式

如果您需要在调用$this->redirectTo后设置setLocale(),请在此调用后创建类的实例:

Lang::setLocale();
$ac = new AuthController();

或者在班级中创建一个特殊方法,用于检查和设置$this->redirectTo

$ac = new AuthController();
Lang::setLocale();
$ac->setRedirectTo();

setRedirectTo()类似于:

public function setRedirectTo()
{
    if ( Lang::getLocale() == 'en' ){
        $lang = '/en';
    } else {
        $lang = '/fa';
    }
    $this->redirectTo = '/home'.$lang;
}

答案 2 :(得分:1)

按照下面的说法:

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectTo;
    protected $lang;

    public function __construct()
    {
        if ( Lang::getLocale() == 'en' ){
            $this->lang = '/en';
        } else {
            $this->lang = '/fa';
        }
        $this->redirectTo = '/home'.$this->lang;

        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    // there is some other methods in here

}