2013-06-28 15 views
2

我有一個小的問題,以下的PHP代碼。在輸入的名字,我想展示自己的當前用戶名,通過$user->username,只有它給了我下面的錯誤:問題與加載用戶類

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

在game_header.php文件我有

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

並認爲它會起作用,但可悲的是它沒有。

我也曾嘗試

$user = new User($_SESSION['id']); 
的edit_account.php頁面上

,但我得到了同樣的錯誤。

這是edit_account.php代碼。

有沒有人知道我可能會在這裏做錯嗎?

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'; 
} 
+0

'session_start();'我沒看到它在任何地方。它是在所有的頁面還是你離開它? –

+0

@Fred the session_start();位於文件頂部的game_header.php中。 – Codemunkie

+0

它必須在所有正在使用的文件中,一般的經驗法則。可能是問題的「一部分」。 –

回答

2

當包裹成一個功能,你必須做到以下幾點:

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

因此,它必須開始:

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

不過,我建議你創建一個參數:

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

那就叫它$user is accessible:

profile_options($user) ; 
+0

謝謝你,這工作差不多。非常感謝! – Codemunkie