【问题标题】:PHP when i call the function i get fatal errorPHP当我调用函数时出现致命错误
【发布时间】:2019-02-16 09:12:30
【问题描述】:

您好,我正在制作一个在终端上运行的 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);




 ?> 

【问题讨论】:

    标签: php function command line


    【解决方案1】:

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

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

    或静态

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

    【讨论】:

      【解决方案2】:

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

      $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
      

      然后你会意识到你的代码中还有其他错误,玩得开心!

      【讨论】:

        猜你喜欢
        • 2011-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多