php - Construction of a singleton class -


an ethical question here.

i'm planning on using several manager classes in new project performing various tasks across whole project. these classes singletons, require construction based on parameters.

as when/where construction has happen, have mixed feelings. have these options far:

option a

it's easy pass these parameters getinstance method while having default null value. on first call parameters used, , additional calls ignore them.

while works, doing feels rather unlogical, following reasons:

  • it makes documentation unclear. getinstance' first parameter must of type collection, can null... what's going on here? can argue writing line in description clear up, i'd prefer clarification unneccesary.

  • it feels faulty pass getinstance construction parameters. due fact method name not explicity hint towards construction, making unclear happen.

option b

i'm thinking setup method. method takes parameters, calls class constructor, , changes internal class state initialized.

when calling getinstance method prior setup, throw notinitializedexception. after setup has been called, additional calls setup result in previouslyinitializedexception.

after setup has been called, getinstance becomes available.

personally, option appeals more me. feels excessive.

what option prefer? , why?

i try , ditch singleton approach , pass manager classes around whatever needs them.

$manager = new manager( $collection, $var, $var2 );  $other_class = new otherclass( $manager ); //or $other_class = new otherclass; $other_class->manager = $manager; //or $other_class = new otherclass; $other_class->setmanager( $manager ); 

Comments