s0c0 Posted April 10, 2007 Share Posted April 10, 2007 I am trying to learn object oriented programming in PHP 5. In doing so I am writing a very simplistic game. There is a Dragon object and a Hero object. The too should be able to exchange damage between eachother by passing eachothers hitpoints to the other and subtracting from their life. So how do I pass one objects variable to the other. I have tried including one class in the other and a bunch of other stuff, the only way I've been able to do it is on the actually page the user sees, but thats not what I want to do. Below is my code. <?php class Hero { public $herolife; public $herohitpoints; public $heroaction; public $herodamage; ///////////////////////////////////////////////////////// // public functions ///////////////////////////////////////////////////////// //// Action - calls additional functions based on user action public function Action($action){ $this->action = $action; if($action == "sword"){ $this->Sword(); } elseif ($action == "arrow"){ $this->Arrow(); } if($action == "evade"){ //Evade(); } else { //Shield(); } } //// Sword - attack with sword yields 5 to 10 hit points public function Sword(){ $this->herohitpoints = rand(5,10); } //// Arrow - attack with bow & arrow yields 0 to 15 hit points public function Arrow(){ $this->herohitpoints = rand(0,15); } } class Dragon { public $dragonlife; public $dragondamage; public $herohitpoints; public function Life(){ //$this->dragondamage = $dragondamage = $herostuff->herohitpoints; //$this->dragonlife = $herohitpoints - $this->dragonlife; if(!isset($this->dragonlife)){ $this->dragonlife = "50"; } } } ?> Question 2: What would be the best way to store each objects life after each round of play? Should these be stored in a url string and then somehow passed back into the class each round, should I use a mysql db? Quote Link to comment Share on other sites More sharing options...
fert Posted April 10, 2007 Share Posted April 10, 2007 to answer your first question I believe the easiest was is to create a new instance of the dragon/hero class and then just minus/add points to their health. As for your second question I believe the best way would be you serialize the objects and store them in a session. Quote Link to comment Share on other sites More sharing options...
s0c0 Posted April 10, 2007 Author Share Posted April 10, 2007 I understand sessions, but have only used them in username type scenario, what do you mean when you say serialize them? Also, what do you mean by creating new instances of my classes? If you think I need to do more reading please point me at a good book. Quote Link to comment Share on other sites More sharing options...
fert Posted April 10, 2007 Share Posted April 10, 2007 sorry, Ignore what I said just add/minus health for the dragon/hero like this $dragon->health-=10; $hero->health-=20; And serialization is the process of converting an object into a format that the object can be recreated from. take a look at the serialize/unserialize functions Quote Link to comment Share on other sites More sharing options...
btherl Posted April 10, 2007 Share Posted April 10, 2007 A class is like a template, or a description. An instance is a specific instance. For example, the Dragon class says what a dragon is like, and how a dragon works. An instance of a dragon is a specific dragon (that your hero is probably fighting). A description of a dragon can't fight a description of a hero. Only an instance of a dragon can fight an instance of a hero. Fert's code reduces the health of a specific dragon called $dragon, and a specific hero called $hero. Somewhere earlier you would have to have code like this: $dragon = new Dragon(); $hero = new Hero(); That takes the the classes that describe what dragons and heroes are, and creates one dragon and one hero from the description. Quote Link to comment Share on other sites More sharing options...
s0c0 Posted April 10, 2007 Author Share Posted April 10, 2007 So let me get this straight. My classes are written in a page called actions.php, the user interacts with the game using index.php. I call these classes from index.php using the require function. So when I call these objects from index.php I am creating an instance of these objects. So I want to create the code that does the "battling" on index.php and store whatever variables in sessions within index.php's code. I want to do this because the classes I created on actions.php simply describes the object and some things the object will do, but what the object does in a given instance is performed within index.php. Am I getting this? Quote Link to comment Share on other sites More sharing options...
s0c0 Posted April 10, 2007 Author Share Posted April 10, 2007 bump...response? Quote Link to comment Share on other sites More sharing options...
btherl Posted April 11, 2007 Share Posted April 11, 2007 Yes, you're getting it Those instances are what you need to store and restore. There's a little catch here with sessions and objects.. When you call session_start(), the class definition must be available. PHP can't restore an instance of class Dragon unless you've included the definition of Dragon already. So make sure you include the Dragon class on every page that will use it, before calling session_start(). So require_once('actions.php'); # Contains class definitions session_start(); # Restore stored classes, now that class definitions are available $dragon = $_SESSION['dragon']; # Get the dragon out from storage $dragon->dance(); # Make the dragon dance! Quote Link to comment Share on other sites More sharing options...
s0c0 Posted April 11, 2007 Author Share Posted April 11, 2007 Sweet, thanks for your help. Note, the dragon will not be doing much dancing lol. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.