Best way to carry & modify a variable through various instances and functions?
Posted
by bobsoap
on Stack Overflow
See other posts from Stack Overflow
or by bobsoap
Published on 2010-06-03T00:00:28Z
Indexed on
2010/06/03
0:04 UTC
Read the original article
Hit count: 207
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!
© Stack Overflow or respective owner