php - How should I return an error (and a message) in large project? -


i'm writing large project, , here's class i'll use often:

class star {     /**      * add      *      * add star something.      *      * @param int $id   id of thing.      */      function add($id)     {         if($this->starred($id))             return 'you starred already.';          if(!$this->existing($id))             return 'the 1 tried star no longer exist.';          $this->db->star($id);          return 'starred successfully!';     } }  $star = new star(); 

but use in different ways like: single page or inside function,

here's problem, sometimes, want know return code not message,

but when use in single page, want return messaage,

so if change add() function this:

function add($id) {     if($this->starred($id))         return 0;      if(!$this->existing($id))         return 1;      $this->db->star($id);      return 2; } 

i can use in functions handle error:

/** leaves comment */ $comment->say('hello.', $id);  /** auto star post because commented on */ if($star->add($id) == 2) {     /** remove comment because post no longer exist */     $comment->remove('hello.', $id);      return 'sorry ; _ ;, post no longer exist.'; } 

but if need return message in many other pages?

i need write code every time?

switch($star->add($id)) {     case 0:         return 'you starred already.';         break;      case 1:         return 'the 1 tried star no longer exist.';         break;      case 2:         return 'starred successfully!';         break; } 

i'm confuse it, appreciated.

for direct solution code read the edit 1 section.

i'm working on rather large project , i'm using errorhandler class made. found working generic error handler class has made easier.

class errorhandler {     /**      * @var string array containing errors set.      */     private static $errors = [];      /**      * set error.      *       * @param string $error - error message you'd set.      * @return string - error being set $errors array.      */     public static function add($error)     {         return self::$errors[] = $error;     }      /**      * errors.      *       * @return boolean if $errors array empty return false, otherwise return errors.      */     public static function get()     {         foreach (self::$errors $error) {             if (empty(trim($error)))                 return false;         }         return self::$errors;     } } 

basically how use this, needed validate form input login, i'd first check if user pressed submit button, i'd use ternary operator run validations , if fails use errorhandler class.

if(isset($_post['login'])) {     $emailaddress = somevalidationshere ? dosomethingwithvalidinput : errorhandler::add("email field empty or format invalid.");     $password = somevalidationshere ? dosomethingwithvalidinput : errorhandler::add("password field can't empty , can't use special characters.");      if(!errorhandler::get()) {                             echo user::login($emailaddress, $password, $autologin);     } else {         $errors = errorhandler::get();          foreach($errors $error) {             echo $error . "<br/>";         }     } }   

so bottom if statement check if errorhandler::get functions not return false in case no need show error message , can progress code, else display error page, way can show multiple errors , have custom formatting.

i prefer method more of long term solution may change id's you'd have go through code , change code manually. gives code sort of structure , keeps code clean.

edit 1

how class? know error codes using const value , can parse error code message using getmessage function. code more understandable , adaptable.

why more...

understandable?

because when (or else) looks @ code see clean name const already_starred_error let developer know instantly error means.

adaptable?

well can change hard coded errors , wouldn't affect code in anyway, if in future wish changed because of spelling mistake or other errors, can change array message.

<?php class star {     const already_starred_error = 1;     const not_found_error= 2;     const successful_entry = 3;      function getmessage($code)     {         $messages = [             1 => "you starred already.",             2 => "the 1 tried star no longer exist.",             3 => "starred successfully!"         ];          return $message[$code];     }      /**      * add      *      * add star something.      *      * @param int $id   id of thing.      */      function add($id)     {         if($this->starred($id))             return self::already_starred_error;          if(!$this->existing($id))             return self::not_found_error;          $this->db->star($id);          return self::successful_entry;     } } ?> 

i'd think edit 1 addressed both issues had.

sometimes, want know return code not message,

but when use in single page, want return messaage,


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -