gpt4 book ai didi

PHP OOP 需要建议

转载 作者:行者123 更新时间:2023-12-04 06:39:04 25 4
gpt4 key购买 nike

我正在构建这个短信通知系统,它会根据特定场合向网络成员(member)发送 10 次免费短信,当某个成员(member)达到 10 次后,系统会发送最后一条通知系统说“这是最后一条免费短信通知”,我目前正在学习 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);

按预期工作(我无法测试这个,因为我目前正在本地构建它)?这是关于 OOP 方法的最佳实践吗?还是我做错了?

我需要建议,非常感谢。

编辑:

当然,在我的原始代码中,我声明了这个类,很抱歉,不在这里写它让每个人都感到困惑: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();
?>

最佳答案

好吧,我认为 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
}
}

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

关于PHP OOP 需要建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com