class Auth extends Controller {
function __constructor(){
private $pass;
}
function Auth()
{
parent::Controller();
$this->load->library('session');
$this->load->helper('cookie');
$this->load->library('email');
}
function index(){
..........
}
function loging(){
$this->pass = "Hello World";
}
function test(){
var_dump($this->pass); // this is on the line 114
}
}
当我访问测试功能时,我收到此错误:
解析错误:语法错误,意外的T_PRIVATE /var/www/clients/client1/web15/web/application/controllers/auth.php on 第6行
而不是字符串“Hello World”。我想知道为什么 ?谁能帮我这个 ? Thx提前
答案 0 :(得分:1)
首先,你不是要创建一个“全局变量”(正如你的标题所示),而是一个私有成员变量。
为此,您需要在构造函数之外声明私有成员变量:
class Auth extends Controller {
private $pass;
function __construct(){
}
function auth()
{
parent::Controller();
$this->load->library('session');
$this->load->helper('cookie');
$this->load->library('email');
}
function index(){
..........
}
function loging(){
$this->pass = "Hello World";
}
function test(){
echo $this->pass;
}
}
此外:
作为一个简单的回答/你要问的例子。试试这个:
<?php
class Auth {
private $pass;
function __construct(){
}
function loging(){
$this->pass = "Hello World";
}
function test(){
echo $this->pass;
}
}
$oAuth = new Auth();
$oAuth->loging();
$oAuth->test();
?>
输出:
C:\>php test.php
Hello World
答案 1 :(得分:0)
像这样:
class Example extends CI_Controller
{
public $variable = "I am Global";
public function test()
{
echo $this->variable; // I am Global
}
public function demo()
{
echo $this->variable; // I am Global
}
}
或者在你的情况下,变量$pass
public
而不是private
public $pass;//<==make change here
使用$this