Love2Code
Love2Code

Reputation: 918

Issue with loading user class

I have a small problem with the following php code. On the input for the name, I'm trying to show their current username, by $user->username, only it gives me the following error:

Notice: Undefined variable: user in /home//domains//public_html/dev/edit_account.php on line 36

Notice: Trying to get property of non-object in /home//domains//public_html/dev/edit_account.php on line 36

in the game_header.php file I have

$user = new User($_SESSION['id']);

And thought it would work, but sadly it does not.

I have also tried

$user = new User($_SESSION['id']);

on the edit_account.php page, but I was getting the same error.

Here's the edit_account.php code.

Does anyone know what I might be doing wrong here?

    include "game_header.php";

    $_GET['type'] = isset($_GET['type']) && ctype_alpha($_GET['type']) ? trim($_GET['type']) : '0';

switch($_GET['type']) {
    case 'profileoptions' : profile_options(); break;
    default : profile_options();
}

function profile_options() {
    echo '
      
    ';
    include 'game_footer.php';
}

Upvotes: 2

Views: 52

Answers (1)

sybear
sybear

Reputation: 7784

When encased into a function you must do the following:

global $user ; //Bring a global variable to the current scope.
echo $user->username ; //Then you can access it and its properties.

So it must start with:

function profile_options() {
  global $user ;
  //The rest of code
}

However, I recommend you to create a parameter:

function profile_options(User $user){
  //Much code
} 

Then call it where $user is accessible:

profile_options($user) ;

Upvotes: 2

Related Questions