Page 1 of 1

OO: Chat Application

Posted: Thu Aug 04, 2005 2:10 pm
by raghavan20
I was quite confident to create a chat application and then I felt that I should develop the application with OO features.

Then lots of doubts have come up and I will ask a few here

I have got two classes in place now:
class Users
class Messages

1. Do I have to create a separate class for object persistance? for more clarity, I am asking whether I have to create a new class which will persist into the database not the 'Users' class or I can create a simple function which will persist user details.

2. How to loop the 'Messages' class to display all messages?

Posted: Thu Aug 04, 2005 3:05 pm
by s.dot
I don't think you should 'loop' the messages. Rather, append them to the chat contents area via javascript.

Edit: I think looping (while trying to grab new messages) would create an infinite loop that would eat up CPU and make the chat very unstable.

Posted: Thu Aug 04, 2005 5:51 pm
by patrikG
Depending on how full of features you want to pack you chat-app, it can very easily become very complex.

reg. 1.: Persistent object are one of the few gripes with PHP. You can implement them with
a) sessions
b) databases (see session_set_save_handler in the manual for examples)
c) a mixture of both

reg. a)
benefits: works for a chat-session, possibly some security-issues
downsides: relies on cookies or trans_sess_id - once the browser is closed all data is lost
reg. b)
benefits: independent of unreliable stuff like cookies, you can store plenty of data
downsides: quite complicated if not used with sessions

I would generally adivse using c) - simply because sessions belong to the basic architecture of a more advanced application and a few small, short and well written (and as McG would rightly argue unit-tested) session-handling classes can be reused anywhere. You can have something akin to persistent sessions (although, unlike Java, you'd have to restart them, but that's a more academic point). No yota of data ever gets lost, you can easily add new feature you didn't think of in the beginning (easy compared to if you had used a) or b) ).

Regarding "looping messages": don't.

Have it event-driven (events triggered client-side), using something like Use AJAX see also http://www.modernmethod.com/sajax/ . Don't ever loop anything if you're waiting for the user to do something (which might never happen anyway).