Problem with Global Variables [duplicate]

那年仲夏 提交于 2020-01-05 11:30:43

问题


I'm somewhat new to OOP programming so it's very likely I'm making some stupid mistake. Here is my problem. When running my code I get the following error:

Fatal error: Call to a member function checkLogin() on a non-object in /application/libraries/Session.php on line 30

Below is the Session.php file (I've commented line 30 to make it easier to find):

<?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;
}
}

In my attempts to troubleshoot the problem I put echo gettype($auth) between the includes and class declaration. The resulting output was "Object." I then tried putting echo gettype($auth) right after I declare global $auth in the startSession function. The resulting output was "NULL." Any idea as to what my problem may be? Thanks.

EDIT: $auth is declared in Authentication.php


回答1:


Without seeing how $auth is defined in Authentication.php, it's hard to help.

Also, since you're new to OOP let me be the first to tell you that globals are bad. Just don't use them. If you feel you have to, most likely it's because you have a bad design. You should rethink that instead of using globals to quick-fix your problem. Instead, instantiate a new instance of your Authentication class by doing something like

$auth = new Authentication;

Make sure Authentication is actually setup properly as a class and everything. PHP's online docs has a good OOP introduction you may want to take a look at, too.




回答2:


The problem isn't in the code that you're actually showing. I'm quite confident that the real issue lies somewhere after the declaration of Session but before you actually create an instance of it. Try inserting var_dump($auth); right before you invoke new Session and see if it's actually set there. Then you can probe the code backwards and locate the point where it's unset.

Also, as have already been mentioned: Globals should be avoided. A better solution would be to pass $auth as a parameter to the constructor of Session and store it as a member variable of the class.




回答3:


The error message:

Fatal error: Call to a member function checkLogin() on a non-object in /application/libraries/Session.php on line 30

means that PHP can't find the checkLogin function in the Authenrication.php. Make sure you instantiate the class found in the Authentication.php to your Session.php or pass the instantiated variable to your Session class.




回答4:


Make sure that $auth is declared global in Authentication.php



来源:https://stackoverflow.com/questions/923707/problem-with-global-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!