dongzanghong4379
dongzanghong4379
2016-07-16 11:12

PHP OOP:在另一个类中使方法可访问的更好实践

已采纳

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;
        }
     ...
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dougou7008 dougou7008 5年前

    Use "dependency injection" to make a Points instance required when instantiating Populate_Fields:

    class Populate_Fields {
      private $points;
    
      public function __construct(Points $pointsObj) {
        $this->points = $pointsObj;
      }
    
      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;
        }
      ...
    }
    

    http://php-di.org/doc/understanding-di.html

    点赞 评论 复制链接分享

相关推荐