0

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');
2
  • 2
    This what you are doing here is not OOP. Stop abusing global state. Commented Nov 27, 2011 at 13:51
  • 2
    Sorry, I thought this was the place where I can ask for directions and be given answers. If you're not going to give an answer then stop replying in respect for those who do. Commented Nov 27, 2011 at 17:03

6 Answers 6

2

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;
Sign up to request clarification or add additional context in comments.

3 Comments

I'm pushing myself to learn OO PHP so I forcefully used a Gender class. :-)
@enchance hmmm, I'm not sure whether using objects really makes sense in this context then. If your only goal is to build a select menu, I'd say it's definitely overkill - an array of strings should do
@enchance , just because you use classes does not make it OOP. Ind this case it is more of a bad design decision.
0

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) {
   ...
}

Comments

0

This is my solution:

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

Comments

0

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

Take a look to: SplObjectStorage

Comments

0
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;
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.