我在建立這個短信通知系統,它會根據特定場合發送10次免費短信給網絡成員,當某個成員達到10次後,系統會發送最後一個通知系統, 「這是最後的免費短信通知」,我目前正在學習PHP的面向對象,並試圖在這個PHP OOP需要建議
使用OOP的形式給出了不進一步在這裏做的是我的代碼:
<?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);
按預期工作(我無法測試這一個,因爲我目前正在建立這個本地)?這是關於面向對象方法的最佳做法嗎?還是我做錯了?
我需要建議,非常感謝。
編輯:
當然對我的原代碼,我聲明類的,我很抱歉,通過不寫在這裏混淆了大家:是指在d,我實際上是尋找一種答案(意見)的我應該對我的代碼(在這種情況下是方法)實施最佳方法(最佳實踐),我擔心的是我不符合KISS或DRY
UPDATE 我設法根據您的建議做一些修改,看起來如何?
<?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();
?>
我更新了我的問題,怎麼樣那?謝謝 – littlechad 2010-12-23 09:39:16