views:

29

answers:

2

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  A: 

I think you need either a instance field.

Matthew Flaschen
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
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
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
Will you have more than one messages object (instance) per script? If so, why?
Matthew Flaschen
Matthew - I'm instantiating the object in index.php, and then again in the __construct() method of each class that uses the object, so that I can access it like $this->message->add() from within that foreign class. Is there a better way?
bobsoap
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](http://en.wikipedia.org/wiki/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
Thanks for this, I'm looking into it now and will post back.
bobsoap
+1  A: 

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
   }
}
phsource