I've worked with PHP for a few years now and have a degree of understanding about classes / objects, but I've never written my own until now.
In that vein then, I'm looking for a bit of confirmation of the code below and that I am doing things properly before I go any further. Any advice / critique is greatly appreciated.
class People {
protected $people = array();
public function __construct() {
$this->people = $people;
$defaults = array(
'model_type' => 'post_type',
'post_type' => 'post'
);
$options = wp_parse_args(get_option('my_options'), $defaults);
$this->model = $options['model_type'];
$this->type = $options['post_type'];
}
public function get_people() {
switch($this->model) {
case 'user' :
$people = get_users();
if(!empty($people)) {
foreach($people as $person) {
$new_person = new Person( $person->ID, $person->first_name, $person->user_email );
$this->people[] = $new_person;
}
}
break;
case 'post_type' :
$args = array(
'posts_per_page' => -1,
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => $this->type,
'post_status' => 'publish',
);
$people = get_posts( $args );
if(!empty($people)) {
foreach($people as $person) {
$new_person = new Person( $person->ID, $person->post_title, get_post_meta($person->ID, '_email_address', true) );
$this->people[] = $new_person;
}
}
}
return $this->people;
}
}
class Person {
public $id;
public $name;
public $email;
public function __construct( $id, $name, $email ) {
$this->id = $id;
$this->name = $name;
$this->email = $email;
}
}