PHP OOP需要建议

时间:2010-12-21 10:02:47

标签: php oop methods

我正在建立这个短信通知系统,该系统将根据特定场合向网络成员发送10次免费短信,并且在某个成员达到10次后,系统将发送最后一个通知系统,说“这是最后一个免费短信通知“,我目前正在学习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 aproach的最佳实践吗?或者我做错了吗?

我需要建议,非常感谢。

编辑:

当然,在我的原始代码中,我宣布了这个课程,我很抱歉,不写这里让所有人感到困惑: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();
?>

4 个答案:

答案 0 :(得分: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 
    }
}

此外,对于记录,您不能从静态方法调用非静态方法。

答案 1 :(得分:1)

您需要将代码包装在类声明中

class SMSNotification {
...
}

您可能还想创建一个构造函数

function __construct() {

这样做的一个原因是,您可以在实例化时将私有变量设置为类。

你实例化这样的类:

$sms = SMSNotification()

您是否将此连接到数据库以进行计数器增量。正如您通常使用oop方法所做的那样,有一个单独的类来处理该连接,因此如果您想构建整个项目,那么一切都将以相同的方式连接到数据库。

您粘贴的两行代码有点不同:

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

你已经使find_member成为一个静态函数(这可能是我想做的),这样你就可以在不创建新类对象的情况下调用该函数。据说它不具有价值$ this,因为它不是当前实例化类的一部分。因此,您需要将其称为(使用我的SMSNotification示例):

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

这告诉php寻找一个名为find_member

的静态函数

另一行代码应该可以正常工作:

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

答案 2 :(得分:0)

根据OOP,您无法在静态成员函数上调用$this->find_member($id)。除了你根本没有声明任何类,所以$this没有意义(据我记得PHP)。你probalby想要声明一些SmsClient类,它将从填充成员变量的db查询中初始化。您的静态find_member($id=0)函数将按ID查询db并返回SmsClient的初始化对象,其id = $id

class SmsClient
{
 private $id;
 private $nSmsSent;

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

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

答案 3 :(得分:0)

听取dewi。

无论如何,测试你是否正在使用正确语法的好方法是注释find_member()status_check()函数的内容并使它们返回一些任意值:如果值实际上是回来了,你做对了。