我写了下面这段代码:
Class stackOverflowExample {
private $hash;
private $cookie_file;
public function __construct(){
@session_start();
if(isset($_SESSION['gc_hash'])){
$this->$hash = $_SESSION['gc_hash'];
}else{
$this->$hash = md5(time());
$_SESSION['gc_hash'] = $this->$hash;
}
$this->$cookie_file = "./cookies/{$this->$hash}.txt";
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误
注意:未定义的变量:第21行的/var/www/gausie/gc/GeoCaching.Class.php中的哈希
致命错误:无法访问第21行/var/www/gausie/gc/GeoCaching.Class.php中的空属性
在原始代码中,第21行引用$this->$hash = $_SESSION['gc_hash'];.
我不明白为什么会发生这种情况,虽然我是OO PHP的新手.有任何想法吗?