1

当我调用我“认为”我已经实例化但显然没有实例化的对象时,我不断收到“未定义变量”通知。我无法指出我的错误。该对象是 $fgmembersite,根据我的错误消息它不存在,我对为什么感到困惑。这可能是我在脚本的 include/require 部分中弄乱了我的目录的一个简单案例,但我一直在查看它们并且看不到任何错误。告诉我你们是否想看看我的文件层次结构。

再次感谢您的帮助!

我有三个正在运行的 PHP 文件。

第一个是 login-home.php

<?PHP
require_once("./profile_settings/view.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}
?>

/*a bunch of stuff*/
<img id="profile_avatar" src="profile_settings/<?php echo fetchAvatarLocation(); ?>"></img>
/*a bunch of stuff*/

接下来我有 view.php,它包含一个函数,用于生成存储图片的路径名。

<?php 
include("./include/membersite_config.php");

    function fetchAvatarLocation()
    {
        $user_id = $fgmembersite->UserId();
        $query = mysql_query("SELECT * FROM ddmembers WHERE id_user = '$user_id'");
        if(mysql_num_rows($query)==0) 
            die("User not found!");
        else
        {
            $row = mysql_fetch_assoc($query);
            $location = $row['imagelocation'];
            return $location;
        }
    }
    ?>

最后我有 membersite_config.php

<?PHP
include("fg_membersite.php");

$fgmembersite = new FGMembersite();

//Provide your site name here
$fgmembersite->SetWebsiteName('Mysitename.com');

//Provide the email address where you want to get notifications
$fgmembersite->SetAdminEmail('My.Email@Provider.net');

//Provide your database login details here:
//hostname, user name, password, database name and table name
//note that the script will create the table (for example, fgusers in this case)
//by itself on submitting register.php for the first time
$fgmembersite->InitDB(/*hostname*/'localhost',
                      /*username*/'user',
                      /*password*/'password',
                      /*database name*/'database',
                      /*table name*/'table');

//For better security. Get a random string from this link: http://tinyurl.com/randstr
// and put it here
$fgmembersite->SetRandomKey('**************');

?>

fg_membersite 是一个包含 fgmembersite 类的文件,其中存储了一堆函数,我需要它的那些是......

function UserId()
    {
        return isset($_SESSION['userid_of_user'])?$_SESSION['userid_of_user']:'';
    }

    function UserName()
    {
        return isset($_SESSION['username_of_user'])?$_SESSION['username_of_user']:'';
    }
4

1 回答 1

2

该对象不在函数的范围内,因此它是未定义的。您需要将其作为参数传递:

function fetchAvatarLocation( $fgmembersite)

然后以对象作为参数调用它:

<img id="profile_avatar" src="profile_settings/<?php echo fetchAvatarLocation( $fgmembersite); ?>"></img>

另一种方法是使用全局变量,但我不推荐它。

于 2012-08-24T19:46:07.133 回答