为什么PHP中的类变量未定义?

时间:2014-04-02 04:31:51

标签: php function class object constructor

在代码的底部,我正在创建一个新的State,并将其命名为$ initialState(第148行)。它看起来像我传递给State构造函数的任何东西都工作..

只是为了确保..我在PHP 5上,我正在使用" function __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

2 个答案:

答案 0 :(得分:0)

您需要使用self关键字来访问静态数据。所以

 $this->stateNum = $prevState; 

通过说这句话,php并没有寻找静态变量,因为它不知道你要求的propoerty是静态的。使用此 -

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

我希望这会对你有所帮助。

答案 1 :(得分:-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.