2010-12-21 99 views
2

我在建立這個短信通知系統,它會根據特定場合發送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(); 
?> 

回答

2

嗯,我認爲OOP主要是創建易於重用的有意義的操作,特別是在幾個月後重新訪問代碼時(或者當別人讀取您的代碼時,很容易找出發生了什麼)這或多或少是相同的)。另外,當你找到你的member時,你可以在那裏執行邏輯,而不是在id上。所以,在這種情況下,它可能會更好,以創建你的方法是這樣,例如:

protected $max_sms_messages = 10; 

protected function can_still_send_sms($member){ 
    return !empty($member) && $member->counter < $this->max_sms_messages; 
} 

public function send_sms($id, $message){ 
    $found = $this->find_member($id); 
    if ($this->can_still_send_sms($found)) { // or even if($found->can_still_send_sms()), if you want to implement it that way 

     //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 
    } 
} 

而且,備案,你不能叫從靜態方法非靜態方法。

+0

我更新了我的問題,怎麼樣那?謝謝 – littlechad 2010-12-23 09:39:16

1

你需要用你的代碼在類聲明

class SMSNotification { 
... 
} 

而且你還可能要創建一個構造這個

function __construct() { 

一個原因是讓你可以設置私有變量實例化時的類。

實例化類是這樣的:

$sms = SMSNotification() 

你會被這個連接的計數器增量的數據庫。正如你通常用一個oop方法做的事情是有一個獨立的類來處理這個連接,所以如果你想建立這個整個項目,那麼所有的東西都會以同樣的方式連接到一個數據庫。

您粘貼的兩行代碼有一點不同:

$found = $this->find_member($id); 

你find_member靜態函數做(這可能就是我會做),這樣就可以調用功能,無需創建新的類對象。這就是說它不是價值$這是因爲它不是當前實例化類的一部分。所以,你需要這樣稱呼它(使用我的SMS通知的例子):

$found = SMSNotification::find_member($id); 

這將告訴PHP將尋找一個名爲find_member

靜態函數的代碼的其他行應該很好地工作:

$status_check = $this->status_check($id); 
+0

我確實聲明瞭類,我只是沒有寫我的問題,我也知道如何實例化,但感謝指出:D – littlechad 2010-12-21 10:33:20

+0

好吧,當然,只是假定因爲你說「這是我的代碼」。你不希望把2括號當宣佈一個類,如上所示「class SmsBonus(){」should be「class SmsBonus {」 – dewy 2010-12-21 11:08:37

+0

哦,是的,錯字我很匆忙:p – littlechad 2010-12-21 13:33:55

0

根據OOP,您不能在靜態成員函數上調用$this->find_member($id)。除了你沒有聲明任何類,所以$this是沒有意義的(據我記得PHP)。你probalby想要聲明一些SmsClient類,它將從db查詢填充成員變量中初始化。你的靜態find_member($id=0)功能將通過ID查詢數據庫,並與它的ID返回SmsClient初始化的對象= $id

class SmsClient 
{ 
private $id; 
private $nSmsSent; 

public __construct($id) 
{ 
    $res = DAL->GetClient($id); 
    //initialize vars here 
} 

public send_sms(...) 
{ 
    $this->nSmsSent++; 
} 
} 
0

聽DEWI。

無論如何,測試是否使用正確語法的一個好方法是註釋掉find_member()status_check()函數的內容,並使它們返回一些任意值:如果實際返回值,則執行該操作對。