Displaying messages to the user

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Displaying messages to the user

Post by Luke »

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

echoing it?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Store to session? Then echo it?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

LOL thanks feyd... I don't really just want to echo it... I realize now that I read the question how stupid it sounds. My question is sort of hard to ask. I think I may have answered it myself already anyway. Thanks
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

put them in a database then echo them out where you want them.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

PrObLeM wrote:put them in a database then echo them out where you want them.
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.

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

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.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

arborint wrote: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.
This is the answer i was looking for... can you elaborate?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The Ninja Space Goat wrote:This is the answer i was looking for... can you elaborate?
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?
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

arborint wrote:
The Ninja Space Goat wrote:This is the answer i was looking for... can you elaborate?
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 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.

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think the goal is to eliminate or control any dependencies that the "5 classes" on might have on any presentation code that would use those messages.
(#10850)
Post Reply