Take the 2-minute tour ×

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:

   $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?


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.

share|improve this question
Could you be a bit more specific? What kind of messages are we talking about here? What kind of scope and timeframe do they exist in? If you have two items $messageOne = new Message(); and $messageTwo = new Message();, do they share data when you go $messageOne->add("Oh noes");? –  Darien Jun 3 '10 at 0:21
Darien - I edited my post above to explain. It's "yes" to your last question: they share data. The scope is the entire script until headers are being sent. Thanks for your help :) –  bobsoap Jun 3 '10 at 0:39
If you're going to have a global messages array (and this, having global variables, is probably not a library), why don't you simply add to it using $messages[] = "Some text";? It seems to be a bit of a waste of a class to do this super-simple operation that's going to be used in only a single script. –  phsource Jun 3 '10 at 1:49
I wanted to use a class because at one point, I want to implement multiple languages. If I have everything in one class, I can later pass it all through a translate function. I'm also generally a little hesitant to use global variables in this manner... I was looking for a static variable that only exists in the message class, can be added to from anywhere at any time, and can only be rendered from that class. I'm looking into dependency injection now (re: Matthew) - I think the main culprit is my approach of re-instantiating dependencies for every class, which makes static vars useless... –  bobsoap Jun 3 '10 at 2:29

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer

I think you need either a instance field.

share|improve this answer
I've looked at static variables, but haven't found a way to implement them for this purpose yet. Would you mind posting a quick example of how exactly I can implement that? Thanks. –  bobsoap Jun 3 '10 at 0:05
I actually re-read your question, and now I'm not clear what you want. Should the message array be per instance, or is it shared by the whole class? I assume it does need to be used outside add. –  Matthew Flaschen Jun 3 '10 at 0:08
The message array should be global (shared by the whole class), and it should be modified via add() in multiple instances. I need to be able to call $message->add() as many times as I like, anywhere in the script, and each time the array is getting a new key added to it. In the end, I need to be able to access the $messages array in order to output it as HTML. Sorry for the confustion, I hope this makes sense now :) –  bobsoap Jun 3 '10 at 0:16
Will you have more than one messages object (instance) per script? If so, why? –  Matthew Flaschen Jun 3 '10 at 0:39
It actually sounds like you only need one object for now, and that you should pass it to the constructor of classes that use it dependency injection. There is no point in constructing multiple objects that have the exact same state. So I think I agree with hsource. –  Matthew Flaschen Jun 3 '10 at 0:45

Your Answer


