Skip to content

Commit 3c0c05e

Browse files
add bad singleton example and remove YAML config
1 parent f6e0fe3 commit 3c0c05e

1 file changed

Lines changed: 34 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ You could write global function like `config()`, but it could clash with another
515515
that tried to do the same thing.
516516

517517
**Bad:**
518+
518519
```php
519520
function config()
520521
{
@@ -524,9 +525,41 @@ function config()
524525
}
525526
```
526527

528+
**Bad too:**
529+
530+
Singleton is a [anti-pattern](https://en.wikipedia.org/wiki/Singleton_pattern).
531+
532+
```php
533+
class Configuration
534+
{
535+
private static $instance;
536+
537+
private function __construct($configuration)
538+
{
539+
// ...
540+
}
541+
542+
public static function getInstance()
543+
{
544+
if (self::$instance === null) {
545+
self::$instance = new Configuration();
546+
}
547+
548+
return self::$instance;
549+
}
550+
551+
public function get($key)
552+
{
553+
// ...
554+
}
555+
}
556+
557+
$singleton = Configuration::getInstance();
558+
```
559+
527560
**Good:**
528561

529-
Create configuration file
562+
Create PHP configuration file or something else
530563

531564
```php
532565
// config.php
@@ -535,16 +568,6 @@ return [
535568
];
536569
```
537570

538-
Or a [YAML](https://en.wikipedia.org/wiki/YAML) configuration file
539-
540-
```php
541-
// config.yml
542-
configuration:
543-
foo: 'bar'
544-
```
545-
546-
Or something else
547-
548571
```php
549572
class Configuration
550573
{

0 commit comments

Comments
 (0)