@@ -525,73 +525,95 @@ function config()
525525}
526526```
527527
528- ** Bad too :**
528+ ** Good :**
529529
530- Singleton is a [ anti-pattern] ( https://en.wikipedia.org/wiki/Singleton_pattern ) .
530+ Create PHP configuration file or something else
531+
532+ ``` php
533+ // config.php
534+ return [
535+ 'foo' => 'bar',
536+ ];
537+ ```
531538
532539``` php
533540class Configuration
541+ {
542+ private $configuration = [];
543+
544+ public function __construct(array $configuration)
545+ {
546+ $this->configuration = $configuration;
547+ }
548+
549+ public function get($key)
550+ {
551+ return isset($this->configuration[$key]) ? $this->configuration[$key] : null;
552+ }
553+ }
554+ ```
555+
556+ Load configuration from file and create instance of ` Configuration ` class
557+
558+ ``` php
559+ $configuration = new Configuration($configuration);
560+ ```
561+
562+ And now you must use instance of ` Configuration ` in your application.
563+
564+ ** [ ⬆ back to top] ( #table-of-contents ) **
565+
566+ ### Don't use a Singleton pattern
567+ Singleton is a [ anti-pattern] ( https://en.wikipedia.org/wiki/Singleton_pattern ) .
568+
569+ ** Bad:**
570+
571+ ``` php
572+ class DBConnection
534573{
535574 private static $instance;
536575
537- private function __construct($configuration )
576+ private function __construct($dsn )
538577 {
539578 // ...
540579 }
541580
542581 public static function getInstance()
543582 {
544583 if (self::$instance === null) {
545- self::$instance = new Configuration ();
584+ self::$instance = new self ();
546585 }
547586
548587 return self::$instance;
549588 }
550589
551- public function get($key)
552- {
553- // ...
554- }
590+ // ...
555591}
556592
557- $singleton = Configuration ::getInstance();
593+ $singleton = DBConnection ::getInstance();
558594```
559595
560596** Good:**
561597
562- Create PHP configuration file or something else
563-
564598``` php
565- // config.php
566- return [
567- 'foo' => 'bar',
568- ];
569- ```
570-
571- ``` php
572- class Configuration
599+ class DBConnection
573600{
574- private $configuration = [];
575-
576- public function __construct(array $configuration)
601+ public function __construct(array $dsn)
577602 {
578- $this->configuration = $configuration;
603+ // ...
579604 }
580605
581- public function get($key)
582- {
583- return isset($this->configuration[$key]) ? $this->configuration[$key] : null;
584- }
606+ // ...
585607}
586608```
587609
588- Load configuration from file and create instance of ` Configuration ` class
610+ Create instance of ` DBConnection ` class and configure it with [ DSN ] ( http://php.net/manual/en/pdo.construct.php#refsect1-pdo.construct-parameters ) .
589611
590612``` php
591- $configuration = new Configuration($configuration );
613+ $connection = new DBConnection($dsn );
592614```
593615
594- And now you must use instance of ` Configuration ` in your application.
616+ And now you must use instance of ` DBConnection ` in your application.
595617
596618** [ ⬆ back to top] ( #table-of-contents ) **
597619
0 commit comments