Page 1 of 1

setting a var = to a session in a class

Posted: Sun Jun 13, 2004 8:51 pm
by dull1554
i have this code

Code: Select all

class projectile_motion {
	var $p1_life;//player one life(human)
	var $p2_life;//player two life(computer)
	var $turn;//determines who goes next
	var $Dx;//distance between opponents
	var $wind_speed;//speed of wind
	var $wind_direction;//direction of wind
	var $image_path = "./images";//without trailing path
	
	function new_session($L1, $L2, $turn = 0) {//initiates a new session, setting player life back to specified level
		$this->p1_life = $L1;
		$this->p2_life = $L2;
		$this->turn = $turn;
	}
	function generate_world() {
		$this->Dx = mt_rand(300,1000);//sets distance between opponents
		$this->wind_speed = mt_rand(0,30);//sets wind speed
		$this->wind_direction = mt_rand(0,1);//sets wind direction(0=west,1=east)
		if($this->wind_speed == 0) {//if wind speed is 0, sets wind direction to 2, meaning no wind in either direction
			$this->wind_direction = 2;
		}
	}
	function build_world() {

Print <<< EOT
<center>
<table border=0>
<tr>
<td width=122></td>
<td width={$this->Dx} height="400"></td>
<td width=122></td>
</tr>
<tr>
<td width=122>
<img border=0 src={$this->image_path}/0_tank.gif width=122 height=65></td>
<td width={$this->Dx}></td>
<td width=122>
<img border=0 src={$this->image_path}/1_tank.gif width=122 height=65></td>
</tr>
<tr>
<td width=122></td>
<td width={$this->Dx}>
<center>{$this->Dx} meters</center></td>
<td width=122>Wind: 
EOT;
if($this->wind_direction == 0) echo "west at ";
if($this->wind_direction == 1) echo "east at ";
if($this->wind_direction == 2) echo "";
Print <<< EOT
{$this->wind_speed}</td>
</tr>
</table>
</center>
EOT;
	}
}
part of a uncompleted class for a projectile motion game.....
to play this game, you have to enter an angle and a power to shoot at then submit, what i need to do is be able to set a var equal to a session or a post $var
i have tried but i cant get it to work

Code: Select all

if(isset($_SESSION['p1_life']))
{
var $p1_life = $_SESSION['p1_life'];
}
but that just returns a error and cant think of any other approach to take
any suggestions/soultions?

Posted: Sun Jun 13, 2004 8:55 pm
by markl999
At the top of the class do var $p1_life;

then where you do var $p1_life = $_SESSION['p1_life']; instead do:
$this->p1_life = $_SESSION['p1_life'];

Posted: Sun Jun 13, 2004 8:57 pm
by dull1554
ok, i did not know that you could use "$this->" outside of a function, and can you use a if statement outside a function...maybe ill make a function to capture all of this

Posted: Sun Jun 13, 2004 8:59 pm
by dull1554
now if i want to set a session inside of a class how would i do that
can i do just

Code: Select all

$_SESSION['blah'] = asldkjfhasldfkj;
or do i have to declare the session a var such as

Code: Select all

var $_SESSION['blah'];
$this->_SESSION['blah'] = lasdjkfhals;

Posted: Sun Jun 13, 2004 8:59 pm
by markl999
Oh, no, you can't use $this outside of a class, i thought this was internal to the class.
Outside of a class you need to do $someobj->p1_life = $_SESSION['p1_life'];

Posted: Sun Jun 13, 2004 9:01 pm
by markl999
$_SESSION['blah'] = asldkjfhasldfkj;
Yes. $_SESSION is a superglobal so it's always in scope, so no need for $this->_SESSION['blah'] ... etc.

Posted: Sun Jun 13, 2004 9:03 pm
by dull1554
ok, and yes i will use it in a class, i just did not know that you could use it out side of a function still in the container class....thats all
thanks for the help