2010-12-21 162 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()函数的内容,并使它们返回一些任意值:如果实际返回值,则执行该操作对。