1

I have a little problem, I will explain everything under the code:

<?php
class Fighter {
    public $name;
    public $health = 100;
    public $power;
    public $mainhand;

    public function Punch($enemy) {
        $arm = rand(0, 1);
        if($this->mainhand == "R") {
            if($arm == 1) {
                $this->power = $this->power * 2;
            }
        }
        else if($this->mainhand == "L") {
            if($arm == 0) {
                $this->power = $this->power * 2;
            }
        }

        switch($arm) {
            case 0:
                $arm = "left";
                $this->power = rand($this->power - 2, $this->power + 2);
                echo $this->name . " hits " . $enemy . " with " . $arm . " arm and deals " . $this->power . " damage";
                break;
            case 1:
                $arm = "right";
                $this->power = rand($this->power - 2, $this->power + 2);
                echo $this->name . " hits " . $enemy . " with " . $arm . " arm and deals " . $this->power . " damage";
                break;
        }

    }
}

$fighter = new Fighter();
$fighter->name = "John";
$fighter->power = 7;
$fighter->mainhand = "R";

$enemy = new Fighter();
$enemy->name = "Matt";
$enemy->power = 6;
$enemy->mainhand = "L";

$fighter->Punch($enemy->name);
echo "<br>";
$enemy->Punch($fighter->name);
echo "<br><br>";

echo $fighter->name . " - " . $fighter->health . "HP";
echo "<br>";
echo $enemy->name . " - " . $enemy->health . "HP";

?>

Let me explain this code, here I have 2 "fighters" (objects), everything works fine, except I want to make that when for example John hits Matt, Matt will lose health, I tried doing like this:

$enemy->health = $enemy->health - $this->power;

But it doesn't work, I get errors:

Notice: Trying to get property of non-object

Warning: Attempt to assign property of non-object

Do you have any ideas how to make the code work the way I want?

1
  • In the future you should point out the lines that are causing the errors. The line numbers are in the error message. Commented Feb 20, 2018 at 21:22

1 Answer 1

2

If you're trying to do that calculation in the Punch method, the problem is that you're not passing the Fighter object when you call Punch. You're passing the string $name property. So use

$fighter->Punch($enemy);

Instead of

$fighter->Punch($enemy->name);

If you pass the Fighter object instead, the calculation you're trying to use should work, but you'll need to make some adjustments to the output in the Punch method, e.g. using $enemy->name instead of $enemy.

echo $this->name . " hits " . $enemy->name . " with " . $arm . " arm and deals " . $this->power . " damage";
//     access the name property here ^^  since $enemy is no longer a string

If you did this by accident, you can catch the mistake earlier in the future by type hinting Fighter in the method signature.

public function Punch(Fighter $enemy) {...
Sign up to request clarification or add additional context in comments.

2 Comments

When I do this, I get error: Catchable fatal error: Object of class Fighter could not be converted to string
Replace $enemy in the echo with $enemy->name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.