Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 832389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:21:20+00:00 2026-05-15T04:21:20+00:00

I’m looking for the best practice way to achieve a message / notification system.

  • 0

I’m looking for the “best practice” way to achieve a message / notification system. I’m using an OOP-based approach for the script and would like to do something along the lines of this:

if(!$something)
   $messages->add('Something doesn\'t exist!');

The add() method in the messages class looks somewhat like this:

class messages {
   public function add($new) {
      $messages = $THIS_IS_WHAT_IM_LOOKING_FOR; //array

      $messages[] = $new;
      $THIS_IS_WHAT_IM_LOOKING_FOR = $messages;
   }
}

In the end, there is a method in which reads out $messages and returns every message as nicely formatted HTML.

So the questions is – what type of variable should I be using for $THIS_IS_WHAT_IM_LOOKING_FOR?

  • I don’t want to make this use the database. Querying the db every time just for some messages that occur at runtime and disappear after 5 seconds just seems like overkill.
  • Using global constants for this is apparently worst practice, since constants are not meant to be variables that change over time. I don’t even know if it would work.
  • I don’t want to always pass in and return the existing $messages array through the method every time I want to add a new message.
  • I even tried using a session var for this, but that is obviously not suited for this purpose at all (it will always be 1 pageload too late).

Any suggestions?

Thanks!

EDIT: Added after I caused some confusion with the above…
The $messages array should be global: I need to be able to add to it through various different classes as well as at the top-level of the whole script.

The best comparison that comes to mind is to use a database to store all the messages that occur at runtime, and when it’s output-time, query the database and output every message. The exception to this comparison is just that the lifetime of the $messages array is the page load (they accumulate during page load, and vanish right after).

So, for example, say I have 10 different actions running one after the other in the script. Each one of these actions make use of a different class. Each one of these classes should be able to post to $messages->add(). After all 10 actions have run, it’s “output time”, and the $messages array can contain up to 10 different messages which were added via all the different classes.

I hope this clarifies it a bit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:21:21+00:00Added an answer on May 15, 2026 at 4:21 am

    I’m not exactly clear about what you want to do, but a good way would be to simply use a private variable:

    class messages {
       private $messages = array();
       public function add($new) {
         $this->messages[] = $new;
       }
       public function output() {
         // Whatever; e.g. a foreach loop that echoes all the messages
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.