1

If one uses autoloader function (with spl_autoload_register), does one need anymore:

  • singleton: all needed classes will be loaded by autoloader anyway, including database, logs, r&r, fronts etc. so need for "always on" etc. object is not clear to me
  • factory: again, autoloader will load required class, so is there a point to use factory class to instantiate worker classes implementing interface, or extending abstract class
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Jeffz
  • 2,075
  • 5
  • 36
  • 51

1 Answers1

1

A singleton is used to hold a single instance of a particular class, so that whenever the instance is retrieved it will always be the same. Therefore, setting data will mean that the data is always the same when retrieving it.

This is different from auto loading a class, as each time you create a new instance you reset the state to the class' default. Still, Singletons should be used sparingly, as they are essentially glorified global variables.

A factory pattern should be used to delegate the determination and creation of objects. It's used when you don't want to specify a concrete class, but require a certain sub-class under a set of conditions. You can't pass construction parameters to an autoloader, and some complex logic may be required to work out which object should be returned from a factory, so again it doesn't achieve the same thing.

An autoloader should just be used to find a class file and include it. It shouldn't (and can't) do any logic such as instantiating an object or making decisions about which object to load.

Jon Cairns
  • 11,783
  • 4
  • 39
  • 66
  • Jonathan ... Factory: makes sense; Singleton: I have problem understanding, why re-creating instance is bad for DB- is singleton really that important here? – Jeffz Mar 12 '12 at 17:04
  • An example for a singleton might be a database connection object, to enforce that only one connection exists at a time. Other examples could be to hold configuration data that is accessible everywhere in an application - e.g. load command line parameters into a singleton called `Config`, and access them anywhere via `Config::read($name)`. – Jon Cairns Mar 12 '12 at 17:14
  • Makes sense. Just one more question if I may. Is it necessary to exclude singletons from autoloader then, since they are instantiated statically? – Jeffz Mar 12 '12 at 17:21
  • The autoloader just finds the class, it doesn't create the object. So no, it isn't excluded from the autoloader. The autoloader is used to find the singleton class, and it's invoked automatically when you call on the class name (e.g. `Singleton::instance()`). This is also true for normal objects: the autoloader is invoked as soon as you declare the class name (generally the constructor, e.g. `new Class()`). Hope that makes sense. – Jon Cairns Mar 12 '12 at 21:51
  • 1
    It does - thank you for your time - much appreciated. For anyone reading this - some more good reading: http://phpadvocate.com/blog/?p=211 – Jeffz Mar 12 '12 at 22:01