在代码点火器中调用全局变量时出错


Error when calling global variables in codeigniter

我想声明一个具有值的全局变量,并在函数中访问它。

我完成了下面的代码,但在登录函数中显示了未定义的变量:msg1

请帮我做这个。

    class loader extends CI_Controller {
    public $msg1 = "login error";
    public function __construct() {
        parent::__construct();
    }
    public function login() {
        $result = $this->login_model->login();
        if (!$result) {
            $msg = $this->$msg1;
            $this->index($msg) ;
        } else {
            //something here
            }
        }
    }
}

试试这个,

class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
    parent::__construct();
}
public function login() {
    $result = $this->login_model->login();
    if (!$result) {
        $msg = $this->msg1;
        $this->index($msg) ;
    } else {
        //something here
        }
    }
}
}

在登录功能中从$msg1中删除$

$msg = $this->msg1;