我对 OOP 编程有点陌生,所以我很可能犯了一些愚蠢的错误。这是我的问题。运行我的代码时,出现以下错误:
致命错误:在第30行的/application/libraries/Session.php 中的非对象上调用成员函数 checkLogin()
下面是 Session.php 文件(我已经注释了第 30 行以便于查找):
<?php
require_once(LIBPATH . 'MySQLDB.php');
require_once(LIBPATH . 'Authentication.php');
class Session {
public $url;
public $referrer;
public $redirect;
function Session() {
$this->startSession();
}
function startSession() {
global $auth;
session_start();
if (isset($_SESSION['url'])) {
$this->referrer = $_SESSION['url'];
} else {
$this->referrer = '/';
}
$this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
if (isset($_SESSION['redirect'])) {
$this->redirect = $_SESSION['redirect'];
} else {
$this->redirect = $_SESSION['redirect'] = '/';
}
$auth->checkLogin(); // LINE 30
}
function setRedirect($page) {
$this->redirect = $_SESSION['redirect'] = $page;
}
}
在我尝试解决问题时,我将 echo gettype($auth) 放在包含和类声明之间。结果输出是“对象”。然后我尝试在 startSession 函数中声明全局 $auth 后立即放置 echo gettype($auth) 。结果输出为“NULL”。知道我的问题可能是什么吗?谢谢。
编辑: $auth 在 Authentication.php 中声明