views:

41

answers:

1

Hi all, basically I have two classes Inventory and Character. During the construct of the inventory I am trying to determine the characters gender however this just doesn't seem to be working for me at all.. I haven't really used static functions before so if somebody could point out what I'm doing wrong it would be much appreciated..

File 1:

class Inventory
{
protected $user_gender;
public function __construct( $id = 0 )
{
$user_gender = Character::getGenderStatic();
}
}

File 2:

class Character
{
protected static $gender;
public static function getGenderStatic() { 
return self::$gender; 
}
}
+1  A: 

In the constructor for Inventory you have

$user_gender = Character::getGenderStatic();

This makes a new variable that's scoped to the constructor. You probably mean

$this->user_gender = Character::getGenderStatic();

which refers to the Inventory object's protected variable you define at the beginning of the class.

I see nothing wrong with the way you're using static functions, except that you haven't set a value for Character::$gender (the protected static variable you define at the beginning of the character class) but I'm assuming you set that somewhere else.

Robert
Thanks Robert but that didn't seem to solve it at all! Gender is set in the character class and is pulled from the database. It has no problems getting gender on normal pages but within this class.... *rolls eyes*
Sean McNamara
How do you know it's not working? What specific result are you expecting, and what is happening instead?
Robert
Basically I'm doing an if statement and if the gender is equal to male it inserts info into the database based on male. In my example Character->user_gender does = 'm' however it just doesn't seem to pull it in $this->user_gender.. However it just uses the else part of my statement and puts in the female info.. Code example: if ($this->user_gender == 'm') { $db->query="male base";) } else { $db->query="female base"); }
Sean McNamara
I actually solved this by passing the Character object to the constructor of Inventory and then just calling in the method from that. Example: private function __construct(Character $char) { $this->user_gender = $char->getGender; }
Sean McNamara