dongzanghong4379 2016-07-16 11:12
浏览 13
已采纳

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 2016-07-16 11:30
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1