加载用户类的问题

时间:2013-06-28 14:28:52

标签: php

我遇到以下php代码的小问题。在输入该名称时,我试图通过$user->username显示其当前用户名,只是它给出了以下错误:

  

注意:未定义的变量:用户在/home//domains//public_html/dev/edit_account.php第36行

     

注意:尝试在第36行的/home//domains//public_html/dev/edit_account.php中获取非对象的属性

在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';
}

1 个答案:

答案 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可访问的位置调用它:

profile_options($user) ;