开发者

Calling a class method from another class..

开发者 https://www.devze.com 2023-01-21 05:25 出处:网络
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 work

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消