What is Best Way to Retrieve Status Messages in Multiple Locations? We like to share the status messages in multiple location in single request, so we retrieve them:

function get_messages(){
    $status_messages = array();
    //loop through each message type
        foreach (drupal_get_messages(null, false) as $type =&gt; $messages) {
               foreach($messages as $key =&gt; $message){
                   $status_messages[$type]['enabled'] = true;
                   $status_messages[$type]['messages'] = array(
                       'message' =&gt; $message
                   );
               }
        }
return $status_messages;
}


Here, since we are passing "FALSE" to the drupal_get_messages function the messages are not reset which is what we like because multiple locations gets to call this function, however. At some point, the messages need to be reset. 


One option is to clear messages at the last point of the request.
Where and how to do that?  
Another option, would implement a cache
and run drupal_get_messages() with "TRUE" to call once and reset at
the same time. I am not familiar with cache in Drupal or PHP. How to
do that?


In short, we like to access status messages in multiple locations and $variable['messages'] is not available to serve the purpose. How to do that? Thank You for help
The solution was using Drupal cache pattern by storing messages in static space as following:

function get_messages(){
    $status_messages = &amp;drupal_static(__FUNCTION__);

    if(!isset($status_messages)){
        //loop through each message type
        foreach (drupal_get_messages(null, true) as $type =&gt; $messages) {
               foreach($messages as $key =&gt; $message){
                   $status_messages[$type]['enabled'] = true;
                   $status_messages[$type]['messages'] = array(
                       'message' =&gt; $message
                   );
               }
        }
     }
     return $status_messages;
}


Here, we get the static reference first and then check if its already set. if not, we retrieve the messages and reset at the same time by calling Drupal API function drupal_get_messages with "true" parameter. As result, we reset the messages that solves our problem and, in addition, our performance is improved due to caching

I have also summarize it all at my blog post "Caching In Drupal"