2014-04-02 262 views
1

朝向代碼的底部,我正在創建一個新狀態,並將其稱爲$ initialState(第148行)。它看起來並不像什麼我傳遞給國家構造工作..爲什麼我的類變量在PHP中未定義?

只是爲了確保..我在PHP 5,我使用「功能__construct」」這就是..之前構建兩個下劃線。

反正有人可以幫忙嗎?..

<?php 

//class for the Tower 
class Tower 
{ 
    public $pegs = array(); //array to represent pegs 

    function __construct(){ 
     //this creates a standard Tower that looks like: 

     // 1 
     // 2 
     // 3 
     // -- -- -- 
     // A B C 

     $this->pegs = array(1,2,3); 
     $this->pegs = array(); 
     $this->pegs = array(); 
    } 

    function copy($t){ 
     //this is a constructor to handle copying 


     $this->pegs = array(0); 
     $this->pegs = array(1); 
     $this->pegs = array(2); 
    } 

    function getSum($peg){ 
     //sum of all disks on peg 

     //if out of range, return -1 for error 
     if ($peg < 0 || $peg > count($peg)){ 
      return -1; 
     } 

     $sum = 0; 


     //loop through the length of a particular peg 
     //sum = values (disk numbers) on that peg 
     for ($i = 0; i < count($this->pegs[$peg]); $i++){ 
      $sum = $sum + $this->pegs[$peg][$i]; 
     } 

     return $sum; 
    } 

    function getPeg($disk){ 
     //gets the peg that a particular disk is on 

     //out of range for disk 
     if($disk < 1 || $disk > 3){ 
      return -1; 
     } 

     //loop through 2D array and look for disk 
     for ($i = 0; $i < count($this->pegs); $i++){ 
      for ($j = 0; $j < count($this->pegs[$i]); $j++){ 
       if ($this->pegs[$i][$j] == $disk){ 
        return ($i+1); 
       } //end if 
      }//end inner for loop 
     }//end outer for loop 
    }//end getPeg fct 


//no equals fct, because PHP has array operators 
} // end of Tower class 

class State 
{//start of State class 
    //purpose is to save the configuration (state) 
    static $prevState = 1; //this will always hold the value of the previous state 
          //we will add 1 to it everytime a state is used 
    public $stateNum; 
    public $g, $h, $f; 
    public $tow, $parentNode; 
    public $stateConfig = array(); 


    function __construct($movedDisk, $g, $h, $t, $s){ 
     //movedDisk = disk that was moved 
     //g = cost to move, or number of disc 
     //h = 6 - sum of discs on target peg 
     //t = will hold our tower 
     //s = will hold our state 

     echo "DOES THIS EVER GET CALLED!!!!"; 

     $this->g = $g; // set state g to G that was passed in 
     $this->h = $h; // set state h to H that was passed in 
     $this->f = $g + $h; //calculate f 
     $this->tow = $t; //set this tower to tower passed in 
     $this->parentNode = $s; //set this parent to parent node (state) passed in 

     //assign appropriate parameters to the stateConfig array 
     $this->stateConfig[] = ($movedDisk); 
     $this->stateConfig[] = ($g); 
     $this->stateConfig[] = ($h); 
     $this->stateConfig[] = ($t->getPeg(1)); 
     $this->stateConfig[] = ($t->getPeg(2)); 
     $this->stateConfig[]= ($t->getPeg(3)); 

     $this->stateNum = $prevState; //assign current stateNum to this new state 
     $prevState++; //increment prevState to be ready for next one 



    } 


    function display(){ 
      echo "DUMPING STATECONFIG ARRAY!!\n"; 
     var_dump ($stateConfig); 
     //echo "State # :" . $this->stateNum . "\n"; 
     //echo "f = " . $this->f . " | " . "g = " . $this->g . " | " . "h = " . $this->h . "\n"; 
     //echo "Current State : " . "{" . $this->stateConfig[1] . "} {" . $this->stateConfig[2] . "} {" . 
       //$this->stateConfig[3] . "} {" . $this->stateConfig[4] . "} {" . $this->stateConfig[5] . "}\n"; 

     for($i = 2; $i >= 0; $i--){ 
      for ($j = 0; $j < 3; $j++){ 
       echo "\t\n"; 
       if (count($tow->pegs[$j]) > $i){ 
        echo " " . $tow->pegs[$j][count($tow->pegs[$j])-$i - 1]. " "; 
       }else{ 
        echo "\n "; 
       }//end if 
      }//end inner for 
     }//end outer for 

    echo "\t--\t--\t--\n"; 
    echo "\t A \t B \t C\n"; 
    echo " \n"; 

    }//end display fct 
}//end of State class 


    static $open = array(); //this will be open array 
    static $closed = array(); //this is closed array 

    echo "----Tower Of Hanoi----\n"; 
    echo "--Solved with A Star--\n"; 

    $initialState = new State (0, 0, 6, new Tower(), null); 
    array_push($open, $initialState); 

    echo "Initial State:\n"; 
    echo "\n"; 
    $initialState->display(); 

    ?> 

錯誤即時得到...

----Tower Of Hanoi---- 
--Solved with A Star-- 
DOES THIS EVER GET CALLED!!!! 
Notice: Undefined variable: prevState in C:\Users\Me\Desktop\TOH.php on line 107 


Notice: Undefined variable: prevState in C:\Users\Me\Desktop\TOH.php on line 108 

Initial State: 

DUMPING STATECONFIG ARRAY!! 

Notice: Undefined variable: stateConfig in C:\Users\Me\Desktop\TOH.php on line 1 
17 
NULL 


Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 



Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 



Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 



Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 



Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 



Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 



Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 



Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 



Notice: Undefined variable: tow in C:\Users\Me\Desktop\TOH.php on line 126 

Notice: Trying to get property of non-object in C:\Users\Me\Desktop\TOH.php on l 
ine 126 

     --  --  -- 
     A  B  C 
+0

它是靜態的,所以你必須使用正確的符號(State :: prevState)來訪問變量。 – ScoPi

回答

-2

$prevState是一個靜態變量。

你需要重寫這個

$this->stateNum = $prevState; //assign current stateNum to this new state 
$prevState++; //increment prevState to be ready for next one 

$this->stateNum = self::$prevState; //assign current stateNum to this new state 
self::$prevState++; //increment prevState to be ready for next one 

此外,

var_dump (stateConfig); 

應該

var_dump ($this->stateConfig); 

您需要在上述上下文中使用$this關鍵字。

也爲此.. $tow變量。您應該$this

if (count($this->tow->pegs[$j]) > $i){ //<--- Like this. 
+2

Lifesaver夥計..感謝這麼多...工作一整天后,這是小事...現在要睡一會兒 – user3476554

+1

**爲什麼downvote?** –

0

您需要使用self關鍵字來訪問靜態數據。所謂

$this->stateNum = $prevState; 

通過這樣說的話,PHP是不是找一個靜態變量,因爲它不知道你問的propoerty是靜態的。使用此 -

$this->stateNum = self::$prevState; 

我希望這可以幫助你。