oop - Representing a status array within a PHP class -


i'm not entirely sure best way handle status field relates integers in database table within class.

say have following:

$status = array(1 => 'active', 2 => 'inactive', 3 => 'cancelled'); 

i'm thinking clarity in system, want class constants , getter retrieving status in associative array, , class uses better defined "1" or "2".

 class user {   const user_status_active = 1;   const user_status_inactive = 2;   const user_status_cancelled = 3;    public function getstatuslist() {    return array(user::user_status_active => 'active',user::user_status_inactive => 'inactive',user::user_status_active => 'user_status_cancelled');   }  } 

this allows setting status using constants with:

class user {  private $status_id;   const user_status_active = 1;  const user_status_inactive = 2;  const user_status_cancelled = 3;   static public function statuslist() {   return array(user::user_status_active => 'active',user::user_status_inactive => 'inactive',user::user_status_cancelled => 'cancelled');  }   public function setstatus($status_id) {   try {    if (!key_exists($status_id, user::statuslist())) throw new exception('invalid status id');    $this->status_id = $status_id;   }   catch (exception $e) {    die($e);   }  } }  $a = new user(); $a->setstatus(user::user_status_active); 

alternately methods can created setactive(), setinactive(), setcancelled().

but i'm trying figure out how best handle actual status values.

would better break statuses out static class?

class user {  private $status_id;   public function setstatus($status_id) {   try {    if (!key_exists($status_id, userstatuses::statuslist())) throw new exception('invalid status id');    $this->status_id = $status_id;   }   catch (exception $e) {    die($e);   }  } }  class userstatuses {  const user_status_active = 1;  const user_status_inactive = 2;  const user_status_cancelled = 3;   static public function statuslist() {   return array(userstatuses::user_status_active => 'active',userstatuses::user_status_inactive => 'inactive',userstatuses::user_status_cancelled => 'cancelled');  } } 

or there entirely different better?

using reflectionclass

i second example; it's simple , effective way make enum type (sort of). actually, of pretty big php orms out there, doctrine operate using similar pattern. 1 thing improve scalability use reflectionclass. allow add new values in form of constants without having change function returns list of values. refactor code check if value valid in enum class keep better separation of concerns between 2 classes

class user {     private $status_id;      public function setstatus($status_id) {         if (userstatuses::isstatus($status_id)) {             $this->status_id = $status_id;         }         else {             throw new exception('invalid status');         }     } }  class userstatuses {     const user_status_active = 1;     const user_status_inactive = 2;     const user_status_cancelled = 3;      public static function statuslist() {         $oclass = new reflectionclass(__class__);         return $oclass->getconstants();     }      public static function isuserstatus($int) {         return in_array($int, self::statuslist());     } } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -