mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-10-01 00:16:42 +02:00
Merge pull request #25 from peter-gribanov/global_functions
Drop singleton
This commit is contained in:
104
README.md
104
README.md
@@ -626,12 +626,13 @@ Polluting globals is a bad practice in very languages because you could clash wi
|
|||||||
library and the user of your API would be none-the-wiser until they get an exception in
|
library and the user of your API would be none-the-wiser until they get an exception in
|
||||||
production. Let's think about an example: what if you wanted to have configuration array.
|
production. Let's think about an example: what if you wanted to have configuration array.
|
||||||
You could write global function like `config()`, but it could clash with another library
|
You could write global function like `config()`, but it could clash with another library
|
||||||
that tried to do the same thing. This is why it
|
that tried to do the same thing.
|
||||||
would be much better to use singleton design pattern and simple set configuration.
|
|
||||||
|
|
||||||
**Bad:**
|
**Bad:**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
function config() {
|
function config()
|
||||||
|
{
|
||||||
return [
|
return [
|
||||||
'foo' => 'bar',
|
'foo' => 'bar',
|
||||||
]
|
]
|
||||||
@@ -639,22 +640,95 @@ function config() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
**Good:**
|
**Good:**
|
||||||
|
|
||||||
|
Create PHP configuration file or something else
|
||||||
|
|
||||||
```php
|
```php
|
||||||
class Configuration {
|
// config.php
|
||||||
private static $instance;
|
return [
|
||||||
private function __construct($configuration) {/* */}
|
'foo' => 'bar',
|
||||||
public static function getInstance() {
|
];
|
||||||
if (self::$instance === null) {
|
```
|
||||||
self::$instance = new Configuration();
|
|
||||||
}
|
```php
|
||||||
return self::$instance;
|
class Configuration
|
||||||
}
|
{
|
||||||
public function get($key) {/* */}
|
private $configuration = [];
|
||||||
public function getAll() {/* */}
|
|
||||||
|
public function __construct(array $configuration)
|
||||||
|
{
|
||||||
|
$this->configuration = $configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
$singleton = Configuration::getInstance();
|
public function get($key)
|
||||||
|
{
|
||||||
|
return isset($this->configuration[$key]) ? $this->configuration[$key] : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Load configuration from file and create instance of `Configuration` class
|
||||||
|
|
||||||
|
```php
|
||||||
|
$configuration = new Configuration($configuration);
|
||||||
|
```
|
||||||
|
|
||||||
|
And now you must use instance of `Configuration` in your application.
|
||||||
|
|
||||||
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
|
### Don't use a Singleton pattern
|
||||||
|
Singleton is a [anti-pattern](https://en.wikipedia.org/wiki/Singleton_pattern).
|
||||||
|
|
||||||
|
**Bad:**
|
||||||
|
|
||||||
|
```php
|
||||||
|
class DBConnection
|
||||||
|
{
|
||||||
|
private static $instance;
|
||||||
|
|
||||||
|
private function __construct($dsn)
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getInstance()
|
||||||
|
{
|
||||||
|
if (self::$instance === null) {
|
||||||
|
self::$instance = new self();
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
$singleton = DBConnection::getInstance();
|
||||||
|
```
|
||||||
|
|
||||||
|
**Good:**
|
||||||
|
|
||||||
|
```php
|
||||||
|
class DBConnection
|
||||||
|
{
|
||||||
|
public function __construct(array $dsn)
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Create instance of `DBConnection` class and configure it with [DSN](http://php.net/manual/en/pdo.construct.php#refsect1-pdo.construct-parameters).
|
||||||
|
|
||||||
|
```php
|
||||||
|
$connection = new DBConnection($dsn);
|
||||||
|
```
|
||||||
|
|
||||||
|
And now you must use instance of `DBConnection` in your application.
|
||||||
|
|
||||||
**[⬆ back to top](#table-of-contents)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
### Encapsulate conditionals
|
### Encapsulate conditionals
|
||||||
|
Reference in New Issue
Block a user