Bugzilla->instance and friends

David Miller justdave at syndicomm.com
Sun Feb 2 06:14:12 UTC 2003

On 1/29/03 9:31 PM +1100, Bradley Baetz wrote:

> Currently, to get a template object, you do:
> my $template = Bugzilla->instance->template
> to get a dbh:
> my $dbh = Bugzilla->instance->dbh
> And so on. IOW, Bugzilla is a singleton class which has references to
> various objects we want to keep arround for some span of time.

Maybe I need some education here as to what a "singleton class" is.  Does
that just mean that the object is the class itself and it doesn't really
instantiate anything?  Or that once instantiated, any other attempt to
instantiate just gives you the first one originally instantiated, so
everyone's talking to the same object no matter if they used "new" or not?

> My original idea for the fix was to do what I'd been planning for a
> while - make the Bugzilla object creation routine take an argument
> indicating what type of program it was, and then only initalise whats
> required. (With defaults so that the majority of cgi files wouldn't
> have to specify anything, and would get Bugzilla::TYPE_CGI
> automaticaly)

This sounds like a cool thing to do.  Might help tinderbox from trying to
open a database connection when running tests, too. :-)

> Then it was mentioned that the ->instance was a bit ugly. Removing that
> points to the fact that we don't really need an object, whch leaves us
> with a whle set of unrelated subroutines.


> So, the alternative is to use Class::Singleton either directly (or just
> use the standard ||= construct), possibly with Apache::Singleton too
> (despite the name, this sort of works without mod_perl, although the
> install tests fail).

So what is Class::Singleton, and what advantage do we get from that outside
of what we'd get from hanging onto the object ID of the created instance
somewhere and just handing that same object ID back to any subsequent
requests for a new Bugzilla object?  This could be accomplished with a "my"
variable within the package definition that isn't stored in the object
hash, couldn't it?

> To get a template, you then:
> Bugzilla::Template->instance

Why not Bugzilla->template ?

> and to get a cgi object:
> Bugzilla::CGI->instance

or Bugzilla->cgi ?

> Code which wants a cgi object then explictly asks for it, and its
> created as needed. Code which doesn't never asks, and so never inits
> stuff it doesn't want.

This would be a good thing anyway.

> (This doesn't actually solve the current problem
> with processmail, because globals.pl gets the cgi object, so we probably
> have to $^0 test that instead. Just forget about that for now, and
> imagine a world where globals.pl is gone. :)

On the other hand, turning processmail into a module is very close to
landing, and that would solve that problem anyway, wouldn't it?  (since the
CGI stream won't be getting re-initialized after it's already been read off
of standard input during a POST)

Dave Miller      Project Leader, Bugzilla Bug Tracking System
http://www.justdave.net/             http://www.bugzilla.org/

More information about the developers mailing list