我在建立这个短信通知系统,它会根据特定场合发送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