为什么我的类变量在PHP中没有定义?
在代码的底部,我创建了一个新的状态,并将其命名为$initialState行148。看起来我传递给状态构造函数的任何东西都不起作用 只是为了确定。。我使用的是PHP5,我使用的是函数构造。。这是构造之前的两个下划线 有人能帮忙吗为什么我的类变量在PHP中没有定义?,php,function,class,object,constructor,Php,Function,Class,Object,Constructor,在代码的底部,我创建了一个新的状态,并将其命名为$initialState行148。看起来我传递给状态构造函数的任何东西都不起作用 只是为了确定。。我使用的是PHP5,我使用的是函数构造。。这是构造之前的两个下划线 有人能帮忙吗 <?php //class for the Tower class Tower { public $pegs = array(); //array to represent pegs 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();
?>
$prevState是一个静态变量
你需要重写这个
$this->stateNum = $prevState; //assign current stateNum to this new state
$prevState++; //increment prevState to be ready for next one
与
此外,
var_dump (stateConfig);
应该是
var_dump ($this->stateConfig);
您需要在上面的上下文中使用$this关键字
也为了这个$两个变量。你应该把这个给我50美元
您需要使用self关键字来访问静态数据。所以-
$this->stateNum = $prevState;
说这句话,php并不是在寻找一个静态变量,因为它不知道您所要求的属性是静态的。用这个-
$this->stateNum = self::$prevState;
我希望这对您有所帮助。它是静态的,所以您必须使用正确的符号State::prevState.Lifesaver dude.非常感谢…在工作了一整天之后,这是一些小事情…现在可以睡一觉了
if (count($this->tow->pegs[$j]) > $i){ //<--- Like this.
$this->stateNum = $prevState;
$this->stateNum = self::$prevState;