I am using two classes: Points and Populate_Fields. The Points class has getters for various points that look like these:
class Points {
public function get_state_points($user_id) {
return $this->calculate_state_points($user_id);
}
public function get_region_points($user_id) {
return $this->calculate_region_points($user_id);
}
...
}
And the Populate_Fields class uses these methods to populate fields:
class Populate_Fields extends Points {
private function populate_state_point_value( $field ) {
$user_id = \thermal\User_Data::get_edited_user_id();
if( ! empty($user_id) ) {
$state_points = $this->get_state_points($user_id);
$field['value'] = $state_points;
update_user_meta($user_id, 'state_point_value', $state_points);
}
return $field;
}
private function populate_region_point_value( $field ) {
$user_id = \thermal\User_Data::get_edited_user_id();
$region_points = $this->get_region_points($user_id);
update_user_meta($user_id, 'region_point_value', $region_points);
$field['value'] = $region_points;
return $field;
}
}
As you can see, currently the Populate_Fields class extends the Points to make these methods available under $this. However, I am not sure if extending is a good practice for this: it does not make much sense to me to make the Populate_Fields a child of Points only because it uses its methods.
Another thing I thought of, is to make an instance of the Points class as a property of the Populate_Fields class:
class Populate_Fields {
private $points;
public function __construct() {
$this->points = new Points();
}
private function populate_state_point_value( $field ) {
$user_id = \thermal\User_Data::get_edited_user_id();
if( ! empty($user_id) ) {
$state_points = $this->points->get_state_points($user_id);
$field['value'] = $state_points;
update_user_meta($user_id, 'state_point_value', $state_points);
}
return $field;
}
...
}
Is it a better practice? Or, if I am using these methods more than in these two classes, does it make sense to make them static instead and use like this:
class Points {
public static function get_state_points($user_id) {
return self::calculate_state_points($user_id);
}
...
}
class Populate_Fields {
private function populate_state_point_value( $field ) {
$user_id = \thermal\User_Data::get_edited_user_id();
if( ! empty($user_id) ) {
$state_points = Points::get_state_points($user_id);
$field['value'] = $state_points;
update_user_meta($user_id, 'state_point_value', $state_points);
}
return $field;
}
...
}