Jump to content

Problem with classes in PHP

- - - - -

  • Please log in to reply
1 reply to this topic

#1
utmmyak

utmmyak

    Newbie

  • Members
  • Pip
  • 1 posts
So, I've been learning PHP for the past few days from a book, and I seem to understand everything, but whenever I try to create my own classes, something goes awry.

Here's the full code of my document:



<!DOCTYPE html 

     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

<meta name="description" content="Php testing. " />

<meta name="keywords" content="phptesting" /> 

<meta name="author" content="Noah Lowenthal" />


<title>Age of Sorcery</title>        

</head>


<body>

<?php // AoS.php

echo "Welcome to The Age of Scorcery.<br />";

global $dummyhealth;

$dummyhealth = 500;

class Bow

{

    public $arrows;

    public $drawn;

    public $shot;

    public $dummyhealth;

    function __construct()

    {

        $this->arrows = "50";

        $this->drawn = "false";

    }

    function draw()

    {

        echo "The bow has been drawn.<\br>";

        $this->drawn = "true";

    }

}

class Wooden_Bow extends Bow

{

    public $attack;

    

    function __construct()

    {

        parent::__construct();

        $this->$attack = 40;

    }

    function shoot()

    {

        echo "The wooden bow has been shot.<\br>";

        $this->drawn = "false";

        $this->shot = "true";

        $dummyhealth -= $attack;

    }


}

class Silver_Bow extends Bow

{   

    public $attack;

    

    function __construct()

    {

        parent::__construct();

        $this->$attack = 60;

    }

    function shoot()

    {

        echo "The gold bow has been shot.<\br>";

        $this->drawn = "false";

        $this->shot = "true";

        $dummyhealth -= $attack;

    }

}

class Gold_Bow extends Bow

{   

    public $attack;

    

    function __construct()

    {

        parent::__construct();

        $this->$attack = 80;

    }

    function shoot()

    {

        echo "The gold bow has been shot.<\br>";

        $this->drawn = "false";

        $this->shot = "true";

        $dummyhealth -= $attack;

    }

}

echo $dummyhealth;

$mygoldbow = new Gold_Bow;

echo "Ready! <\br>";

$mygoldbow->draw();

$mygoldbow->shoot();

echo $dummyhealth;        

?>           


</body>

</html>

I keep getting a "Notice: Undefined variable: attack in C:\web\AoS.php on line 76

Fatal error: Cannot access empty property in C:\web\AoS.php on line 76" error whenever I run it.

I'm sorry if this is a really trivial error, but I can't seem to find what I did wrong.

I'd really appreciate help.

Thanks,
Noah

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hello,

You must remember that properties (even if they are variables) are not accessed as variables, therefor you must use $this->attack in your construct, much as you have done in others. Your error is that $attack is not a variable, so $this->null cannot be performed (and is empty.)

Try to fix those statements and see if your class works as expected.

$dummyhealth referenced in a few of your shoot() methods also will access local variables rather than of the parent class, if that is not what you have intended.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users