PHP OOP 需要建议

发布于 2024-10-08 18:20:39 字数 2982 浏览 3 评论 0原文

我正在构建这个短信通知系统,它将根据特定情况向网络会员发送10次免费短信,并且在某个会员达到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

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();
?>

i am building this sms notification system, which will send 10 times free sms based on certain occasion to the web's member, and after a certain member reach 10 times, the system would send a last notification system saying that "this is the last free sms notification", i am currently learning PHP OOP and trying to use an OOP aproach on this

without further a do here's my code:

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

}
?>

would this lines:

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

work as expected (i cant test this one out because i am currently building this on local)? and is this a best practice regarding OOP aproach ? or am i doing this wrong ?

i need advice, thank you very much.

EDIT:

of course on my original code i declare the class, i am sorry that by not writing it here confuses everybody :D, i am actually looking for a kind of answer (advice) that pointed the way i should implement a best approach (best practice) on my codes (in this case methods), things that i worry about is that i don't meet the requirements such as K.I.S.S or D.R.Y.

UPDATE
i manage to do some modifications based on your suggestions, how is this looks like ?

<?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();
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

古镇旧梦 2024-10-15 18:20:39

好吧,我认为 OOP 主要是关于创建易于重用的有意义的操作,特别是当您几个月后重新访问代码时(或者当其他人阅读您的代码时,这或多或少)可以轻松地找出发生了什么。不太相同)。此外,当您找到您的成员时,您可以对其执行逻辑,而不是对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 
    }
}

另外,根据记录,您不能从静态方法调用非静态方法。

Well, I think OOP is mostly about creating meaningful actions that are easy to reuse and, especially, make it easy to find out what's going on when you revisit your code some months later (or when someone else reads your code, which is more or less the same). Also, when you found your member, you can then perform logic on that, instead of on the id. So, in this case it might be better to create your methods like this, for example:

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

Also, for the record, you can't call non-static methods from static methods.

寂寞花火° 2024-10-15 18:20:39

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

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);

You need to wrap your code in a class declaration

class SMSNotification {
...
}

And you may also want to create a constructor

function __construct() {

One reason for this is so that you can set private variables to the class when it is instantiated.

you instantiate the class like this:

$sms = SMSNotification()

Will you be connecting this to a database for the counter increment. As what you would normally do with an oop approach is have a seperate class that handles that connection, so that if you wanted to build on this whole project then everything would be connecting to a database the same way.

The two lines of code you pasted are a bit different:

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

you have made find_member a static function (which is probably what i would have done) so that you can call the function without create a new class object. That being said it is not of value $this as it is not part of the current instantiated class. So you will need to call it like (using my example of SMSNotification):

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

This tells php to look for a static function called find_member

The other line of code should work fine:

$status_check = $this->status_check($id);
乱了心跳 2024-10-15 18:20:39

根据 OOP,您不能在静态成员函数上调用 $this->find_member($id) 。此外,您根本没有声明任何类,因此 $this 毫无意义(据我记得 PHP)。您可能想要声明一些 SmsClient 类,该类将从填充成员变量的数据库查询中初始化。您的静态 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++;
 }
}

According to OOP you can't call $this->find_member($id) on a static member function. Besides you didn't declare any class at all, so $this is meaningless (as far as I remember PHP). You probalby wanted to declare some SmsClient class which would be initialized from db query filling in member variables. Your static find_member($id=0) function would query db by id and return initialized object of SmsClient with its id = $id

class SmsClient
{
 private $id;
 private $nSmsSent;

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

 public send_sms(...)
 {
  $this->nSmsSent++;
 }
}
╰◇生如夏花灿烂 2024-10-15 18:20:39

听杜威的话。

无论如何,测试您是否使用正确语法的一个好方法是注释掉 find_member()status_check() 函数的内容,并让它们返回一些任意值值:如果实际返回值,则说明您做得正确。

Listen to dewi.

Anyway, a nice way to test if you're using the right syntax is commenting out the content of the find_member() and status_check() functions and make them return some arbitrary values: if the values are actually returned, you're doing it right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文