Featured post
PHP OOP need advice -
i building sms notification system, send 10 times free sms based on occasion web's member, , after member reach 10 times, system send last notification system saying "this last free sms notification", learning php oop , trying use oop aproach on this
without further here's code:
<?php class smsbonus { //bonus_sms fields = id, member_id, counter, end_status public static function find_member($id=0){ //query find member } public function add_counter($id=0){ //query increment value of counter field } public function status_check($id=0){ //query check whether given member's counter has reach 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 sms notification saying member has reach end of bonus period //update member's end_status table 1 }else{ //send regular notification } } } ?>
would lines:
$found = $this->find_member($id); $status_check = $this->status_check($id);
work expected (i cant test 1 out because building on local)? , best practice regarding oop aproach ? or doing wrong ?
i need advice, thank much.
edit:
of course on original code declare class, sorry not writing here confuses :d, looking kind of answer (advice) pointed way should implement best approach (best practice) on codes (in case methods), things worry don't meet requirements such k.i.s.s or d.r.y.
update manage modifications based on suggestions, how looks ?
<?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 find member } public function add_counter($id=0){ //query increment value of counter field } public function status_check($id=0){ //query check whether given member's counter has reach number 10 } public function update_status($id=0){ //query update when member reach 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 sms notification saying member has reach end of bonus period $this->update_status($id); }else{ //send regular notification $this->add_counter($id); } } } $sms_bonus = new smsbonus(); ?>
well, think oop creating meaningful actions easy reuse and, especially, make easy find out what's going on when revisit code months later (or when else reads code, more or less same). also, when found member
, can perform logic on that, instead of on id
. so, in case might better create methods this, 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 if($found->can_still_send_sms()), if want implement way //send sms notification saying member has reach end of bonus period //update member's end_status table 1 }else{ //send regular notification } }
also, record, can't call non-static methods static methods.
- Get link
- X
- Other Apps
Comments
Post a Comment