私はこのsms通知システムを構築しています。これは、特定の機会に基づいて10回の無料SMSをWebメンバーに送信し、特定のメンバーが10回到達した後、システムは、 「これが最後の無料のSMS通知である」、私は現在、PHP OOPを学習し、さらにここで行うことなく、このPHP OOPはアドバイスが必要です
にOOPのaproachを使用しようとしていますと、私のコードです:
<?php
class SmsBonus {
//bonus_sms fields = id, member_id, counter, end_status
public static function find_member($id=0){
//query to find a certain member
}
public function add_counter($id=0){
//query to increment the value of counter field
}
public function status_check($id=0){
//query to check whether the given member's counter has reach the number 10
}
public static function send_sms($id, $message){
$found = $this->find_member($id);
$status_check = $this->status_check($id);
if(!empty($found) && !empty($status_check) && $found->counter == 10){
//send the sms notification saying that this member has reach the end of the bonus period
//update this member's end_status table to 1
}else{
//send the regular notification
}
}
}
?>
だろう、この行:
$found = $this->find_member($id);
$status_check = $this->status_check($id);
は期待どおりに動作します(私は現時点ではローカルでこれを構築しているため、これをテストできません)。これはOOPに関するベストプラクティスですか?または私はこれを間違っている?
アドバイスが必要です、どうもありがとうございます。
EDIT:私の元のコードのコースの
私はクラスを宣言し、私はここでそれを書いていないことにより、誰もが混乱することを申し訳ありません:Dを、私は実際に指摘答えのようなもの(アドバイス)を探しています私のコード(この場合はメソッド)で最良のアプローチ(ベストプラクティス)を実装する必要があります。心配するのは、私がKISSやDRYなどの要件を満たしていないことです
更新 私はあなたの提案に基づいていくつかの変更を行うことができますが、どのように見えますか?
<?php
class SmsBonus{
//bonus_sms fields = id, member_id, counter, end_status
protected $max_sms = 10;
public $id;
public $member_id;
public $counter;
public $end_status;
public function find_member($id=0){
//query to find a certain member
}
public function add_counter($id=0){
//query to increment the value of counter field
}
public function status_check($id=0){
//query to check whether the given member's counter has reach the number 10
}
public function update_status($id=0){
//query to update when a certain member reach its sms bonus limit
}
protected function can_still_send_sms($member_id){
$found = $this->find_member($member_id);
$status_check = $this->status_check($id);
return !empty($found) && $found->counter < $this->max_sms && !empty($status_check);
}
public function send_sms($id, $message){
$phone = Phone::find_member($id); //
if ($this->can_still_send_sms($id)) {
//send the sms notification saying that this member has reach the end of the bonus period
$this->update_status($id);
}else{
//send the regular notification
$this->add_counter($id);
}
}
}
$sms_bonus = new SmsBonus();
?>
私は質問をどのように更新する必要がありますようにクラスを宣言するときに2つの角かっこを置くしたくないそれ ?ありがとう – littlechad