Displaying messages to the user
Moderator: General Moderators
Displaying messages to the user
Say I have like 5 classes... all of which may generate errors, or messages for the user. What is the best way to take all of these messages and print them to the appropriate places? I am really trying to keep everything as portable and reusable as possible as well as clean and neat. What is a common solution for displaying important information to a user?
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
The problem here is that as soon as you bring a database into something as basic as displaying error messages you're increasing the complexity of reusing the code.PrObLeM wrote:put them in a database then echo them out where you want them.
I agree with Maugrim. Keep them in a session so you have a log and echo them. I've played around trigger_error() in classes but it just causes confusing since the error details point to the parts of the class that triggered the error, not the location where the user called the method.
Using debug_backtrace() you can actually work out where the user made the method call that triggered the error.
I sometimes just have an error container (an array) inside the object that only has a lifetime as long as the object exists. I spit out errrors and/or log them in the container.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
I would invert the problem. Give each class a getErrorMsg() method and give your View access to the objects -- then the View can sort out how to best display the messages. You could also have a middle-man logging object that collects the messages as well to provide a place to hold them through the program flow. Sometimes the Request or Response objects are used for this as well.
Also, I often make my error messages un-punctuated so the presentation can add a period for singles or commas when combining multiple messages.
Also, I often make my error messages un-punctuated so the presentation can add a period for singles or commas when combining multiple messages.
(#10850)
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
set_error_handler() and set_exception_handler() would be useful.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
The trick is not going to be creating a container to hold the messages (plus some context info about them perhaps). The problem is how to make that container and your other objects come in contact with each other at error message time. I am assuming that your "5 classes" are probably Model classes so it really matters on your archiecture as to how those messages get to the presentation code. Are all 5 always present or just one of the 5 on any request?The Ninja Space Goat wrote:This is the answer i was looking for... can you elaborate?
(#10850)
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
The plugin stuff I did recently could do this. You'd load in the error handling object, trigger an error event and have the error handling object do it's task. Incidentally, this method also wouldn't care if the objetcs are present or not, it would just be ignored like any other event handler in other languages (JS?) if there are no implemented methods.arborint wrote:The trick is not going to be creating a container to hold the messages (plus some context info about them perhaps). The problem is how to make that container and your other objects come in contact with each other at error message time.The Ninja Space Goat wrote:This is the answer i was looking for... can you elaborate?
I'm thinking I could be educated on a pattern I know little about here now. Correct me if I'm wrong, but can the observer pattern handle tasks like this? And if it can does anybody want to give a really simple example?
EDIT | Elaborated on "event handling" style system.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US