views:

281

answers:

4

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

+3  A: 

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.

Marc W
+1 Globals are bad, they'll be gone in 6 and it's bad practice to use them anyway.
Phil Carter
Quick question. It seems to me it would be bad to keep instantiating new instances of certain classes such as a database class or session class. I've done a bit of reading and it seems like a solution to this is using a Singleton Pattern. Would you agree with using a Singleton Pattern or is their a better method? Thanks for your help.
blcArmadillo
Singletons are perfect for those types of situations, where there *really* needs to be only one global instance of a class because that class provides a single service to the rest of the application. The singleton pattern is also one of the most abused patterns, so make sure you don't overuse it.
Marc W
Global variables will not be gone from PHP 6. Maybe the syntax changes, but they won't be gone. On a related theme, Singletons are just a different syntax for global variables.
troelskn
A: 

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

adept
You don't declare variables as global in PHP. Global is only used to import a variable into the scope of the function.
Emil H
+1  A: 

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.

Emil H
A: 

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.

marknt15