Where should we store confirmation/error messages in application? Where should we store confirmation/error messages in application? php php

Where should we store confirmation/error messages in application?


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?


How about storing it in some non-direct coding language and then use a macro for transforming this into an array or static class or whatever you like? It may be in the database, XML or whatever you'd like. You would only need 2 fields for an error message: the name of the message and the content.

For example, in XML you might do this:

<messages>  <message>    <name>PRODUCT_SAVED_OK</name>    <content>Product no. %d has been saved successfully</content>  </message>  ...</messages>

Then have some process to transform this into arrays or the thing that best fits your project. For example, I would save this to an array:

$messages = array(  'PRODUCT_SAVED_OK' => 'Product no. %d has been saved successfully',  ...);

And for dealing with variables inside the messages, I'd do printfs:

if ($saved_ok){  printf($messages['PRODUCT_SAVED_OK'], $product_number);}

Of course, encapsulate this inside a Message class for better and cleaner code. One drawback of the printf solution is that you need to know in advance which parameters do any message need. If you'd like to translate the message and some message with more than one parameter gets them inverted in another language, printf can deal with this:

// In both cases, day would be 3rd parameter to printf, month the second and year the first$spanish = 'Hoy es %3$d/%2$d/%1$d';$english = 'Today is %2$d/%3$d/%1$d';


The first thing you have to go and look at is what your problem is.

For you it seems like code maintenance. What I have found to work is storing all these messages as a separate class for example called Notifications (just an example though)

Every type of notification you would call can be stored as a method so when you call the notifier it would get some message from class members which can easily be changed according to your needs.

Storing these messages in a database is not a bad way of doing it, just not the real solution to your need at this point. That means having to maintain another table adding extra queries to your code which have to be escaped properly etc. In short, more maintenance instead of less.

I would go for the class in which you can store these messages as class constants.

Also, members of this class can very easily be translated so it would be your best option.