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

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料