@@ -626,35 +626,109 @@ Polluting globals is a bad practice in very languages because you could clash wi
626626library and the user of your API would be none-the-wiser until they get an exception in
627627production. Let's think about an example: what if you wanted to have configuration array.
628628You could write global function like ` config() ` , but it could clash with another library
629- that tried to do the same thing. This is why it
630- would be much better to use singleton design pattern and simple set configuration.
629+ that tried to do the same thing.
631630
632631** Bad:**
632+
633633``` php
634- function config() {
634+ function config()
635+ {
635636 return [
636637 'foo' => 'bar',
637638 ]
638639}
639640```
640641
641642** Good:**
643+
644+ Create PHP configuration file or something else
645+
646+ ``` php
647+ // config.php
648+ return [
649+ 'foo' => 'bar',
650+ ];
651+ ```
652+
653+ ``` php
654+ class Configuration
655+ {
656+ private $configuration = [];
657+
658+ public function __construct(array $configuration)
659+ {
660+ $this->configuration = $configuration;
661+ }
662+
663+ public function get($key)
664+ {
665+ return isset($this->configuration[$key]) ? $this->configuration[$key] : null;
666+ }
667+ }
668+ ```
669+
670+ Load configuration from file and create instance of ` Configuration ` class
671+
672+ ``` php
673+ $configuration = new Configuration($configuration);
674+ ```
675+
676+ And now you must use instance of ` Configuration ` in your application.
677+
678+ ** [ ⬆ back to top] ( #table-of-contents ) **
679+
680+ ### Don't use a Singleton pattern
681+ Singleton is a [ anti-pattern] ( https://en.wikipedia.org/wiki/Singleton_pattern ) .
682+
683+ ** Bad:**
684+
642685``` php
643- class Configuration {
686+ class DBConnection
687+ {
644688 private static $instance;
645- private function __construct($configuration) {/* */}
646- public static function getInstance() {
689+
690+ private function __construct($dsn)
691+ {
692+ // ...
693+ }
694+
695+ public static function getInstance()
696+ {
647697 if (self::$instance === null) {
648- self::$instance = new Configuration ();
698+ self::$instance = new self ();
649699 }
700+
650701 return self::$instance;
651702 }
652- public function get($key) {/* */}
653- public function getAll() {/* */}
703+
704+ // ...
654705}
655706
656- $singleton = Configuration ::getInstance();
707+ $singleton = DBConnection ::getInstance();
657708```
709+
710+ ** Good:**
711+
712+ ``` php
713+ class DBConnection
714+ {
715+ public function __construct(array $dsn)
716+ {
717+ // ...
718+ }
719+
720+ // ...
721+ }
722+ ```
723+
724+ Create instance of ` DBConnection ` class and configure it with [ DSN] ( http://php.net/manual/en/pdo.construct.php#refsect1-pdo.construct-parameters ) .
725+
726+ ``` php
727+ $connection = new DBConnection($dsn);
728+ ```
729+
730+ And now you must use instance of ` DBConnection ` in your application.
731+
658732** [ ⬆ back to top] ( #table-of-contents ) **
659733
660734### Encapsulate conditionals
0 commit comments