Global Class

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
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Global Class

Post by Benjamin »

What is the procedure for a class that meets the following criteria:

1. May be called from many other classes
2. Generally, only 1 instance is needed, but sometimes more (ie cannot be a Singleton
3. Has already been instantiated before any other classes

Using a global var would be easy but I'm not sure that is the proper method.

I don't want to instantiate it everytime I need it, but sometimes I will need to have 2 or more copies of it running concurrently. Would it be best to simply make a copy of it?

EDIT: Also, what would be the best way to call this class from other classes?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

either pass a reference to it when creating the objects (or calling their methods) ... or use a registry. The latter doesn't seem to fit well though since you need to deal with multiple instances.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Search for Factory and Registry.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

A class that can have one or more instantiated objects and is not a Singleton is just a plain old class. If you want it created before it is used then you have the option of creating an instance and passing it into where you will use it either directly or via a Registry/Factory. You can also Lazy load the class/object by supplying the "wiring" to instansiate it to something like Service Locator and then it will be created the first time it is asked for.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Registry for me. You can still use multiple instances but you can also use the registry to keep fetching the same instance if you want.
Post Reply