in reply to How can I avoid creating two instances of the same object

Have a singleton hold your database connection (roll your own or use something like Class::Singleton). The User and Company classes use that singleton to get access to the database handle.

Keep in mind, however, that there may be reentrancy issues if (for example) you need to have multiple active statement handles simultaneously (depending on your database, and presuming you're using DBI).

Replies are listed 'Best First'.
Re^2: How can I avoid creating two instances of the same object
by beachguy82 (Novice) on Mar 02, 2006 at 03:54 UTC
    This is the first time I've looked at that moudule, but I'm not sure that that's what I'm looking for. Would that allow me to call methods from different subclasses, to be used on one object. I'll write a little sudo code. client will be a method of Company and user will be a method of User.
    use Company; use User; my $dbh = Company->new(); my $client_info = $dbh->client(); my $user_info = $dbh->user();
    Keep in mind that Company has gained it's new method by subclassing a parent class. I don't even know if this is possible, but it sure would be nice!

      Erm, you've just muddied the waters a good bit. Unless Company is a subclass of User why would you be able to call a method from the later on the former?

      At any rate, it looks like you might be more interested in Class::DBI (which I should have mentioned in the first place). Define your DBI connection parameters in a base class and then all of your table classes inherit from that and will do the right thing with regards to sharing a database handle.