2012-06-15 171 views
-5

我確信這個問題有一個簡單的答案,但對於我的生活,我找不到它。我使用PHP5,我試圖改變一個類變量的值,但它似乎沒有改變。例如,我有這樣的東西:如何在PHP中更改類變量?

<?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是未定義的。我並沒有將所有的代碼都包含在內,因爲我認爲我只是誤解了類變量是如何聲明的。

+3

你已經包括了類,但不是代碼調用類函數。你可以添加一個小片段,展示如何實例化foo並調用echoVariable()? –

+0

代碼中有語法錯誤,例如__construct缺少開啓花括號。我解決了它,並試圖在我的本地服務器上,它運行良好,你沒有運行代碼時出現錯誤? – Ryan

+3

這是實際的代碼?您的構造函數不會被PHP調用,因爲關鍵字需要雙下劃線和圓括號('__construct()')。請顯示**實際**代碼。 – CodeCaster

回答

1

您有一個錯字。使用__construct。兩個(_ _)下劃線。你的fooVariable永遠不會被設置。它被設置後,它工作正常。

更新:您只在您的功能draw()方法中設置$this->yweightUser方法。如果您在ajax_post()或w/e之前沒有撥打draw(),則不會被定義。

+1

另外,添加'(){':-P –

+0

對不起,這不是實際的代碼。我用我的實際代碼更新了這個問題。我只是覺得我沒有完全理解如何聲明類變量。 – thuey

+0

已更新的答案。 –

1

正如上面提到的,固定的語法errrors和它的作品,2個下劃線的構造函數和左括號的構造,也括號內爲參數:

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();