Jump to content

[SOLVED] passing variables between classes?


s0c0

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.