dongmaoluan5719
2011-11-27 13:48
浏览 6
已采纳

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');
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • doushi1960 2011-11-27 15:38
    已采纳

    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. :-)

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doumu1873 2011-11-27 13:51

    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;
    
    评论
    解决 无用
    打赏 举报
  • dongyingjiu0669 2011-11-27 13:51

    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) {
       ...
    }
    
    评论
    解决 无用
    打赏 举报
  • douju2053 2011-11-27 13:54
    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;
    }
    
    评论
    解决 无用
    打赏 举报
  • douzhonglong3789 2011-11-27 13:54

    This is my solution:

    $_genders = array('Male','Female','Alien');
    $gender = array();
    foreach($_genders as $g)
    {
       $gender[$g] = new Gender($g);
    }
    
    评论
    解决 无用
    打赏 举报
  • donglian4879 2011-11-27 13:55

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

    Take a look to: SplObjectStorage

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题