PHP OOP:迭代从类创建的所有对象

I want to create a <select> field from all Gender objects. Is there a way to iterate over all the objects created from the class of Gender?

class Gender {

    public static $counter = 0;
    public $id;
    public $gender;

    public function __construct($gender){
        Gender::$counter++;
        $this->id = Gender::$counter;
        $this->gender = $gender;
    }

}

// Objects
$gender_male = new Gender('Male');
$gender_female = new Gender('Female');

I understand now that it's an extreme overkill to use a class but for the sake of knowing how it's done, here's the new code (based on all your comments):

class Gender {

    public static $counter = 0;
    public static $genders = array();

    public function __construct($gender){
        // Here it is
        Gender::$genders[++Gender::$counter] = $gender;
    }

}

// Objects
$gender_male = new Gender('Male');
$gender_female = new Gender('Female');

It's a group achievement in its own way but I think I'll switch to arrays instead. :-)

Is there a way to iterate over all the objects created from the class of Gender?

To a degree, yes, but it's a very bad idea design-wise.

Why not put all the relevant objects you want to query into an array?

$genders = array();
$genders["male"] = new Gender('Male');
$genders["female"] = new Gender('Female');

you can then walk through each element using

foreach ($genders as $gender)
 echo $gender->id;

Something like this you could do.

class Gender {

    public static $genders = array();
    public $gender;

    public function __construct($gender){
        $this->gender = $gender;
        self::genders[] = $this;
    }

}

// Objects
$gender_male = new Gender('Male');
$gender_female = new Gender('Female');

foreach(Gender::genders as $gender) {
   ...
}
class Gender {

    public static $counter = 0;
    public $id;
    public $gender;
    private static $instances = array();

    public function __construct($gender){
    Gender::$counter++;
    $this->id = Gender::$counter;
    $this->gender = $gender;
    self::$instances[] = $this;
    }

    public static function getInstances(){
    return self::$instances;
    }


}

new Gender( "male" );
new Gender( "male" );

foreach( Gender::getInstances() as $genderInstance ) {
echo $genderInstance->gender;
}

This is my solution:

$_genders = array('Male','Female','Alien');
$gender = array();
foreach($_genders as $g)
{
   $gender[$g] = new Gender($g);
}

Maybe a Container-Class would fit perfect for this task.

Take a look to: SplObjectStorage