Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should we store confirmation/error messages in application?

I am working on PHP and Zend. I have to show different type of error/confirmation messages in application. Most of these messages are placed in code. So if I have to change one message then I have to change it everywhere where this particular type of message is coded.

So what is the best way to store all messages in on place and use them across the application.

Possible solutions:

  • Store all messages in database. (We have to move these messages to other database type. For example if we move from MySQL to SQL Server.)

  • Store all messages in separate php class using array and get these messages using class methods (Problem: We can not use this class in other programming languages.)

  • Store message in special format which acceptable for all languages for example ini type of files.

EDIT: (After looking Ozair's Answer)

Sometimes we have to change message for particular record. For example:

Product No. 10 is deleted.
Product No. 15 is deleted.

What will be best method to handle this case ?

Thanks

like image 854
Student Avatar asked May 31 '11 07:05

Student


People also ask

When should you display error messages?

In order to display error messages on forms, you need to consider the following four basic rules: The error message needs to be short and meaningful. The placement of the message needs to be associated with the field. The message style needs to be separated from the style of the field labels and instructions.

Where do I show form errors?

The two most common placements for error messages are at the top of the form and inline with erroneous fields. Which placement is more intuitive for users? A research study discovered that displaying all error messages at the top of the form puts a high cognitive load on user memory.


1 Answers

You could have some sort of static class in the library folder. Which would contain a set of constant variables like

PRODUCT_SAVED_OK = "The product {{id}} was saved successfully";

The I would have two static methods which take care of displaying the error/message

public static function showMessage($message , $data = array("id" => "10"){
    foreach($data as $key => $value){
        $message = str_replace("{{" . $key . "}}", $value , $message);
    }
    echo $message;
}

public static function showError($error){
   echo $error;
}

Than in your code whenever you need to display the message you simply invoke the static class like so...

Messages::showMessage(Messages::PRODUCT_SAVED_OK);

That way all the messages the application needs will be contained in one class and if need be that the message changes you only have to change it this class.

What do you think?

like image 126
Gabriel Spiteri Avatar answered Oct 28 '22 15:10

Gabriel Spiteri