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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP当我调用函数时我得到了致命的错误相关的知识,希望对你有一定的参考价值。

嗨,我正在制作一个在终端上运行的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, ("
");
echo $swordman -> health, ("
");
echo $swordman -> strength, ("
");
echo $swordman -> defence, ("
");
echo $swordman -> speed, ("
");
echo $swordman -> luck, ("
");
}elseif($input =='brute'){
$comb1 = $brute;

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

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

$comb1 = $grappler;

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

 }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, ("
");
echo $swordman -> health, ("
");
echo $swordman -> strength, ("
");
echo $swordman -> defence, ("
");
echo $swordman -> speed, ("
");
echo $swordman -> luck, ("
");
}elseif($input =='brute'){
$comb2 = $brute;

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

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

$comb2 = $grappler;

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

}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

然后,您将意识到代码中存在其他错误,请玩得开心!

以上是关于PHP当我调用函数时我得到了致命的错误的主要内容,如果未能解决你的问题,请参考以下文章

PHP 连接和查询错误:致命错误:在 null 上调用成员函数 query()

致命错误:调用未定义的函数oci_connect()php错误

致命错误:未捕获的错误:调用成员函数 find() PHP simple_html_dom_parser [重复]

PHP“致命错误:未捕获错误:调用成员函数prepare()为null”

致命错误:未捕获错误:在null上调用成员函数select()

致命错误:在第 2 行的 index.php 中调用未定义函数 get_header()