How to configure the initialization of the Singleton?

0 like 0 dislike
41 views
There is a library where is the place to be the class, which within the same process allowed the existence of only one instance, i.e. is a Singleton. The trouble is that the initialization of this instance has several dynamic parameters that need somehow to pass.


The solution should work on PHP > 5.2. The only thought that came to mind:

public static function init($params) { if(self::$instance) { throw new Exception(__CLASS__ . 'already initialized'); } $class = __CLASS__; self::$instance = new $class($params); } public static function getInstance() { if(!self::$instance) { throw new Exception(__CLASS__ . 'is not initialized'); } return self::$instance; }



But it seems by far the most successful. Does anyone have any more ideas, adelitas?

Thank you!
by | 41 views

7 Answers

0 like 0 dislike
Not to be attached to a singleton (something that actually exists in only one instance does the class alone, especially considering that its behavior depends on the parameters of the constructor). In my opinion, the most logical would be not to use singleton, the only instance of this class to store in the registry (create the instance somewhere in the code depends on the architecture of your application).
by
0 like 0 dislike
en.wikipedia.org/wiki/Multiton_pattern
\ren.wikipedia.org/wiki/Immutable_object
\r
Sorry of course but if you need is single — yuzayte Multition (Onge pool of singles) if you want to change the state of the object you created 1 again — to try and use immutable (Immutable)
by
0 like 0 dislike
by
0 like 0 dislike
If the class is singleton, then the parameters are global within the application or session.
1. Maybe he finds out its configuration from an external source?
2. Make a method/class is a wrapper that collects (itself) the parameters and transmitting them to the constructor for the singleton, and then return the same instance. Wrapper at the application level, so that the encapsulation does not suffer.
by
0 like 0 dislike
Here is an example of a singleton in PHP
\r
 public static function singleton() { if (!isset(self::$instance)) { echo 'Creating new instance.'; $className = __CLASS__; self::$instance = new $className; } return self::$instance; } 

It seems obvious to do something like that
\r
 public static function singleton($params) 

Actually you get about what you described.
I think it's normal.
by
0 like 0 dislike
<?\rclass MySingleton\r{\r\rstatic private $_instance;\r\rprivate function __construct($params)\r{\r//При необходимости, недостающие параметры обработать тут.\r$this->_params = $params; } public static function getInstance() { if (!self::$_instance instanceof self) { self::$_instance = new self(func_get_args()); } return self::$_instance; } ... } MySingleton::getInstance('param1', 'param2', 'paramN')->doSomething(); 

In General, it is not kosher.
\r
Yuzayte Registry.
by
0 like 0 dislike
Divide the constructor and parameters:
\r
class Singleton{ static public punction getInstance(){ ... } public function applyParams($params){ ... } } $singleton = Singleton::getInstance()->applyParams($param1); // some code // change param $singleton->applyParams($param2); 
by

Related questions

0 like 0 dislike
7 answers
asked Mar 24, 2019 by azverin
0 like 0 dislike
7 answers
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
32,925 users