全局变量的问题 [英] Problem with Global Variables

查看:109
本文介绍了全局变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对OOP编程有点新,所以很可能我犯了一个愚蠢的错误。这是我的问题。当运行我的代码时,出现以下错误:


致命错误:调用成员函数checkLogin()在 /application/libraries/Session.php 上的非对象 30


下面是Session.php文件(我已经评论过第30行以便于查找):

  <?php 
require_once(LIBPATH。'MySQLDB.php');
require_once(LIBPATH。'Authentication.php');

class Session {
public $ url;
public $ referrer;
public $ redirect;

函数Session(){
$ this-> startSession();
}
函数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(); //行30
}
函数setRedirect($ page){
$ this-> redirect = $ _SESSION ['redirect'] = $ page;






$ b

在我尝试排除这个问题时,我把echo gettype ($ auth)在包含和类声明之间。结果输出为Object。然后,我在startSession函数中声明全局$ auth之后立即尝试放置echo gettype($ auth)。结果输出为NULL。任何想法可能是我的问题?
$ b

编辑:$ auth在Authentication.php中声明

没有看到 $ auth 是如何在 Authentication.php 中定义的,很难帮助。

另外,由于您是OOP的新手,请让我第一个告诉您全局变差。只是不要使用它们。如果你觉得你必须这样做,很可能是因为你的设计不好。您应该重新思考,而不是使用全局变量来快速修复您的问题。相反,通过做一些类似于

  Authentication 类的新实例> $ auth =新认证; 

确保认证实际设置正确一个班级和一切。 PHP的在线文档有一个很好的 OOP介绍,您可能也想看一看。


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

解决方案

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.

这篇关于全局变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆