-5

我确信这个问题有一个简单的答案,但对于我的生活,我找不到它。我正在使用 PHP5,我正在尝试更改类变量的值,但它似乎没有改变。例如,我有这样的事情:

<?php

include_once "../includes/global.php";

loadDAOs('yweightGroups','yweightCourses','yweightUsers','user','yweightData');



class yweight {

private $header_includes;
private $user;
private $yweightUser;

//Description:
//  Constructor.
//Parameters:
//  -none-
//Returns:
//  -nothing-
function __construct() {
    global $r_action;
    global $r_page;

    $this->user = login_required(); 

    if(!$this->user || empty($this->user)){
        die();
        goHome();   
    }

    $this->header_includes = "";

    if(isset($r_action)) {
        $this->$r_action(); 
    }
    else if (isset($r_page)) {
        $this->$r_page();   
    }
    else {
        $this->draw();  
    }       
}

//Description:
//  Draws the YWeight page
//Parameters:
//  -none-
//Returns:
//  -nothing-
function draw() {
    global $r_page;     
    global $colorDAO;
    global $yweightUsersDAO;

    ob_start();
    ?>
    <script type='text/javascript' src="./yweight.js"></script>
    <link rel="stylesheet" type="text/css" href="yweight.css" />
    <?php

    $this->header_includes .= ob_get_clean();

    $col = $colorDAO->read(17);
    print_top("",$this->header_includes,"resources","",$col->value,$col->fontcolor);

    $users = $yweightUsersDAO->GetByUserID($this->user->id);

    if(!$users){
        echo "<div class='msg_failed'>You are not authorized to view this page.</div>";
        die();
    }

    $this->yweightUser = $users;
    echo serialize($this->yweightUser[0]);
    ?>
    <div id="yweight_area"></div>
    <script type='text/javascript'>
        <?php
        if($users[0]->yweightUsers_intern == 0)
            echo "drawPage('main_participant');";
        else
            echo "drawPage('main_intern');";
        ?>
    </script>
    <?php   
    print_bottom();
    echo ob_get_clean();

}           


//Description:
//  Draws the main intern page.
//Parameters:
//  -none-
//Returns:
//  -nothing-
function main_intern() {
    ob_start();
    echo "hello intern";
    echo ob_get_clean();
}

//Description:
//  Draws the main participant page.
//Parameters:
//  -none-
//Returns:
//  -nothing-
function main_participant() {
    global $yweightDataDAO;
    ob_start();
    $this->yweightUser = $this->yweightUser[0];
    echo serialize($this->yweightUser);
    ?>
    <div id="banner_div">
        <img class="banner" width="927" src="images/ParticipantsMain.jpg" />
        <img id="tracker_btn" class="std_btn" src="images/trackerButton.png" />
        <img id="summary_btn" class="std_btn" src="images/summaryButton.png" />
        <img id="requirements_btn" class="std_btn" src="images/reqButton.png" />
    </div>
    <div id="discussion_board_input_div">
        <textarea id="discussion_board_input" />
        <?php
        echo build_btn("green",100,25,"Submit","","","","",
            'submit_post()',"margin-top:5px; margin-bottom:5px; float:right;");
        ?>
    <div class="clear_floats" />
    </div>
    <div id="discussion_board_div">

    </div>
    <?php
    echo ob_get_clean();
}

function submit_post(){
    global $yweightDataDAO;
    global $userDAO;
    global $r_post;
    echo serialize($this->yweightUser);
    $userDataID = $yweightDataDAO->GetByUserID($this->yweightUser->user_id);

    if($userDataID){
        $userData = $yweightDataDAO->read($userDataID);
        $userDiscussion = unserialize($userData->yweightData_discussionBoard);
    }

    $userDiscussion[$this->user->firstName .  time()] = $r_post;

    $userData->yweightData_discussionBoard = serialize($userDiscussion);

    $yweightDataDAO->save($userData);
}

}


$recs = new yweight();

?>

submit_post() 是一个 ajax 调用函数。我得到的错误是它说 $this->yweightUser 未定义。我最初并没有包含所有代码,因为我认为我只是误解了类变量的声明方式。

4

2 回答 2

1

你有一个错字。使用__construct. 两个 ( _ _) 下划线。你fooVariable永远不会被设置。设置好之后就可以正常使用了。

更新:你只$this->yweightUser在你的函数draw()方法中设置。draw()如果您之前或 w/e不调用ajax_post(),它将不会被定义。

于 2012-06-15T15:29:00.453 回答
1

如上所述,修复语法错误并且它可以工作,构造函数的两个下划线和构造函数的左大括号以及参数的括号:

  class foo {

protected $fooVariable;

function __construct () {
  $this->fooVariable = "hello world";
  $this->changeVariable();
}

function changeVariable(){
  $this->fooVariable = "goodbye world";
  $this->echoVariable();
}

function echoVariable(){
  echo $this->fooVariable;
}
}

$foo = new foo();
于 2012-06-15T15:30:10.977 回答