Calling a class method from another class..
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 does开发者_如何学Pythonn'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;
}
}
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.
精彩评论