PHP当我调用函数时,我得到了致命错误

PHP当我调用函数时,我得到了致命错误,php,function,command,line,Php,Function,Command,Line,嗨,我正在做一个PHP游戏,在终端上运行。(基于文本的游戏)在php上 当我调用代码最底层的函数时,我得到一个致命错误。(我正在尝试获取用户的输入并将其添加到变量中,以便函数BattleStart可以调用它,并且每次它都将具有所选角色的属性) 这是代码 <?php class Combatant{ public $name; public $health; public $strength; public $defence; public $speed; public $l

嗨,我正在做一个PHP游戏,在终端上运行。(基于文本的游戏)在php上

当我调用代码最底层的函数时,我得到一个致命错误。(我正在尝试获取用户的输入并将其添加到变量中,以便函数BattleStart可以调用它,并且每次它都将具有所选角色的属性)

这是代码

<?php 




  class Combatant{

public $name;
public $health;
public $strength;
public $defence;
public $speed;
public $luck;

function setName($x){
    $this -> name = $x;
    }


}

class Battle extends Combatant{



    function BattleStart($comb1,$comb2){

       $damagecomb1 = $comb1 -> strength - $comb2 -> defence;
       $damagecomb2 = $comb2 -> strength - $comb1 -> defence;

       if ($comb1 -> health > $comb2 -> health){
        $comb2 -> health = $comb2 -> health - damagecomb1;


    }elseif ($comb2 -> health > $comb1 -> health) {
        $comb1 -> health = $comb1 -> health - damagecomb2;

    }elseif ($comb1 -> health == $comb2 -> health) {

        if ($comb1 -> defence < $comb2 -> defence){
            $comb2 -> health = $comb2 -> health - damagecomb1;

        }elseif ($comb1 -> defence > $comb2 -> defence) {
            $comb1 -> health = $comb1 -> health - damagecomb2;
        }
    }


    for ($rounds <=30; $rounds >=1; $rounds++){


    }
}





}

//swordman attributes
$swordman = new Combatant;

$swordman -> setName('swordman');
$swordman -> health = rand(40,60);
$swordman -> strength = rand(60,70);
$swordman -> defence = rand(20,30);
$swordman -> speed = rand(90,100);
$swordman -> luck = rand(0.3, 0.5);





 //Brute attributes
$brute = new Combatant;

 $brute -> setName('brute');
 $brute -> health = rand(90,100);
 $brute -> strength = rand(65,75);
 $brute -> defence = rand(40,50);
 $brute -> speed = rand(60,80);
 $brute -> luck = rand(0.3, 0.35);




 //Grappler
 $grappler = new Combatant;


 $grappler -> setName('grappler');
 $grappler -> health = rand(60,100);
 $grappler -> strength = rand(75,80);
 $grappler -> defence = rand(35,40);
 $grappler -> speed = rand(60,80);
 $grappler -> luck = rand(0.3, 0.4);




 echo "Choose your Character: ";

 $input ='';
 $input = trim(fgets(STDIN,1024));

  $comb1;
  $comb2;

if ($input == 'sword') {
$comb1 = $swordman;


echo $swordman -> name, ("\n");
echo $swordman -> health, ("\n");
echo $swordman -> strength, ("\n");
echo $swordman -> defence, ("\n");
echo $swordman -> speed, ("\n");
echo $swordman -> luck, ("\n");
}elseif($input =='brute'){
$comb1 = $brute;

echo $brute -> name, ("\n");
echo $brute -> strength, ("\n");
echo $brute -> defence, ("\n");
echo $brute -> speed, ("\n");
echo $brute -> health, ("\n");
echo $brute -> luck, ("\n");

}elseif ($input == 'grappler') {

$comb1 = $grappler;

echo $grappler -> name, ("\n");
echo $grappler -> health, ("\n");
echo $grappler -> strength, ("\n");
echo $grappler -> defence, ("\n");
echo $grappler -> speed, ("\n");
echo $grappler -> luck, ("\n");

 }else {
echo 'you didnt type the right thing.. Try again';
 }


  echo "Choose your Oponent: ";

  $input1 ='';
  $input1 = trim(fgets(STDIN,1024));


if ($input == 'sword') {
$comb2 = $swordman;


echo $swordman -> name, ("\n");
echo $swordman -> health, ("\n");
echo $swordman -> strength, ("\n");
echo $swordman -> defence, ("\n");
echo $swordman -> speed, ("\n");
echo $swordman -> luck, ("\n");
}elseif($input =='brute'){
$comb2 = $brute;

echo $brute -> name, ("\n");
echo $brute -> strength, ("\n");
echo $brute -> defence, ("\n");
echo $brute -> speed, ("\n");
echo $brute -> health, ("\n");
echo $brute -> luck, ("\n");

}elseif ($input == 'grappler') {

$comb2 = $grappler;

echo $grappler -> name, ("\n");
echo $grappler -> health, ("\n");
echo $grappler -> strength, ("\n");
echo $grappler -> defence, ("\n");
echo $grappler -> speed, ("\n");
echo $grappler -> luck, ("\n");

}else {
echo 'you didnt type the right thing.. Try again';
}

BattleStart($comb1, $comb2);




 ?> 

BattleStart是一个类的一部分,您需要首先添加一个实例,或者将函数设置为静态

$battle = new Battle();
$battle->BattleStart($comb1, $comb2);
或静态

Battle::BattleStart($comb1, $comb2);

在使用对象的任何方法或属性之前,需要实例化该对象

$play = new Battle(); // Instantiante with "new" keyword
$play->BattleStart(comb1, $comb2); // Call BattleStart method
或更短

(new Battle)->BattleStart($comb1, $comb2); // Instantiate Battle and call BattleStart method
然后,您将意识到代码中还有其他错误,祝您玩得开心