我有这个对象的实例吗?

时间:2012-08-24 19:44:28

标签: php sql variables undefined

当我调用一个我认为'我已经实例化过的对象时,我一直收到'未定义的变量'通知,但显然没有。我不能指责我的错误。对象是$ 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']:'';
    }

1 个答案:

答案 0 :(得分:2)

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

function fetchAvatarLocation( $fgmembersite)

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

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

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