-1

Could anyone tell me why my character stats will not print? I'm not getting any error or syntax messages..

This is my first php project ever. I can't figure out what I'm missing!

<?  
    class character{
        public $healthpoints = 100;
        public $isdead = false;
        public $class = "Mage";
        public $level = 10;
    }


    function checkdeath() {
    if($healthpoints >= 0){
        $isdead = true;
        }   
    }

    new character();

    function CharStats() {
        echo $healthpoints;
        echo $isdead;
        echo $class;
        echo $level;
    }

    CharStats;


    ?>
DragonKyn
  • 69
  • 8
  • This a variable scope issue - http://php.net/manual/en/language.variables.scope.php. Your class variables are not in scope of your function. Easy solution is to add your function to your class as a class method – Sean Jul 02 '17 at 02:07
  • It starts with reading the error messages: https://3v4l.org/he30R – hakre Jul 02 '17 at 02:15

1 Answers1

2

I can't figure out what I'm missing!

Thinking, ALL

class Character{
    public $healthpoints = 100;
    public $isdead = false;
    public $class = "Mage";
    public $level = 10;

    function checkdeath() {
       if($this->healthpoints >= 0){
        $this->isdead = true;
      }   
    }

    function CharStats() {
        echo $this->healthpoints;
        echo $this->$isdead;
        echo $this->$class;
        echo $this->$level;
    }

}

$character = new Character();
$character->ChatStats();

Read again about classes/objects/etc. - Classes and Objects in PHP

voodoo417
  • 11,861
  • 3
  • 36
  • 40
  • A class method should not echo anything. Perhaps out of scope, but returning a sttring, then proxied via __toString() and then echo'ing the object variable might be (more) appropriate.. – hakre Jul 02 '17 at 02:21
  • Excellent, thanks guys I got it working exactly as I wanted. I changed my $isdead to a string that is merely now yes or no. – DragonKyn Jul 02 '17 at 02:24