Search code examples
phpobjectirc

How to track multiple instances of the same user (object)


I'm writing an IRC bot in PHP, and I wanted to make a s little more sophisticated then most other bots.

I have the following classes in my application:

  1. Bot - The actual bot class, it handles all of the functionality and commands the bots can get.
  2. Registry - Global settings and variables, passed on to any functioning class so that I don't have to litter the global namespace with variables.
  3. Channel - Defines the channel object, properties: $name, $nicklist, where $name is the channel name and $nicklist is an array of User objects.
  4. User - Defines the user object, properties: $nickname, $fullAddress, where $fullAddress is the user's address in the form of nick!user@host.

So far so good, however, a user may be on multiple channels, and since I don't want to have multiple objects from the same user (as if a user, for instance, changes his nickname, I'll have to update multiple elements instead of just one).

How would I go about this, I was thinking about holding a grand user pool at the base of the Bot class, and pass these references to the channels, but I don't really know how to do that either :P

Can anyone point me in the right direction? I can paste my current code if you'd like (it's rather long so I rather not do it if no one needs it).

Thanks.


Solution

  • Since the Bot object is the one that needs to do the tracking, have an array of User objects on it, and pass the pointers to those objects (aka $bot->users['name']) to the Channel object.

    That way, the same pointer can be passed twice in case the user is already found on the users list.