【问题标题】:Fatal error: Uncaught Error: Call to a member function致命错误:未捕获的错误:调用成员函数
【发布时间】:2018-09-10 05:30:50
【问题描述】:

尝试从 Apache 提供我的 PHP 代码时出现此错误:

注意:未定义变量:COMMUNITY in /var/www/Account/assets/includes/head/nav.php 在第 1 行

致命错误: 未捕获错误:调用成员函数 getUser() on /var/www/Account/assets/includes/head/nav.php:1 中的 null 堆栈跟踪: #0 /var/www/Account/assets/includes/head.php(60): include_once() #1 /var/www/Account/cpanel/community.php(3): 包括('/var/www/Accoun...')#2 /var/www/Account/assets/classes/core.php(14): require_once('/var/www/Accoun...') #3 /var/www/Account/assets/includes/head.php(7): require_once('/var/www/Accoun...') #4 /var/www/Account/cpanel/home.php(3): include('/var/www/Accoun...') #5 {main} 在线抛出 /var/www/Account/assets/includes/head/nav.php 1

在 Windows 中的 XAMPP 上它正在工作,我没有收到错误;但在带有 Apache 的 Linux 上却没有。两者都使用 PHP 7.1


head.php中的内容:

<!DOCTYPE html>
<?php
include_once("config/min.php");

session_start();
define('ROOTPATH', __DIR__);
require_once(ROOTPATH.'/../classes/core.php');

if ($USER->is_logged_in())  {
  $stmt = $USER->runQuery("SELECT * FROM users WHERE id=:uid");
  $stmt->execute(array(":uid"=>$_SESSION['userSession']));
  $U_DATA = $stmt->fetch(PDO::FETCH_ASSOC);
}

?>
<html>
  <head>

    <title><?php echo $title; ?></title>

    <?php include_once("head/styles.php"); ?>

    <?php include_once("config/ads.php"); ?>
  </head>

    <body>
    <?php include_once("head/nav.php"); ?>

core.php中的内容

<?php

require_once 'init.php';
require_once 'PasswordStorage.php';


require_once 'user.php';
$USER = new USER();

require_once 'community.php';
$COMMUNITY = new COMMUNITY();

?>

nav.php中的内容

<?php if (!strpos($_SERVER['REQUEST_URI'], "a/")) { if ($USER->is_logged_in()) { $C_USER = $COMMUNITY->getUser($U_DATA['id']); } ?>
<div class="wrapper">
...

community.php中的内容

<?php

class COMMUNITY
{
    private $conn;
    public function __construct() {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    private function request($path) {
        $url = "https://community.makeroid.io";
        $api_key = "XXX";
        $api_user = "system";
        $request = file_get_contents($url."/".$path."?api_key=".$api_key."&api_username=".$api_user);
        if ($request) {
            return json_decode($request, true);
        } else {
            return false;
        }

    }

    public function getUser($id) {
        $stmt = $this->conn->prepare("SELECT * FROM c_users WHERE u_id=:id;");
        $stmt->bindparam(":id",$id);
        $stmt->execute();
        $user = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if (count($user) == 1) {
            return $user[0];
        } else {
            return null;
        }
    }

    public function verifyUser($username, $id, $email) {
        $emails = $this->request("users/".$username."/emails.json");
        if ($emails) {
            if ($emails['email'] == $email) {
                $stmt = $this->conn->prepare("INSERT INTO c_users (u_id, username) VALUES(:uID, :username);");
                $stmt->bindparam(":uID",$id);
                $stmt->bindparam(":username",$username);
                $stmt->execute();
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function getUserData($username) {
        $user = $this->request("users/".$username.".json");
        return $user['user'];
    }

    public function getProfilePicture($username) {
        $user = $this->request("users/".$username.".json");
        return "https://community.makeroid.io".$user['user']['avatar_template'];
    }

    public function getId($username) {
        $user = $this->request("users/".$username.".json");
        return $user['user']['id'];
    }

    public function getTrustLevel($username) {
        $user = $this->request("users/".$username.".json");
        return $user['user']['trust_level'];
    }

    public function getTrustLevelRequirements($id) {
        $tl3_requirements = $this->request("admin/users/".$id.".json");
        if ($tl3_requirements['tl3_requirements']) {
            return $tl3_requirements['tl3_requirements'];
        } else {
            return false;
        }
    }
}

?>

为什么 $USER 和 $COMMUNITY 不共享?

【问题讨论】:

  • 您可以将 移到 html 之前。
  • 可能是 COMMUNITY 类构造函数存在问题,导致它返回 NULL?尝试在“core.php”中的类实例化之后添加 exit('
    '.print_r($COMMUNITY,1).'
    ');
    看看你得到什么输出在每个系统上。
  • require_once(ROOTPATH.'/../classes/core.php'); 是否指向正确的文件?
  • @Averias 是的
  • @Nathan 如果我评论 COMMUNITY 的 require_once 并且会话开始它会起作用

标签: php apache


【解决方案1】:

我终于通过在 core.php

中的设置解决了问题
<?php
require_once(ROOTPATH.'/../classes/user.php');
$USER = new USER();

require_once(ROOTPATH.'/../classes/community.php');
$COMMUNITY = new COMMUNITY();

似乎没有得到正确的文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2016-04-20
    • 2020-08-24
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2017-07-16
    相关资源
    最近更新 更多