Skip to content

Commit

Permalink
First stable Selecton version.
Browse files Browse the repository at this point in the history
  • Loading branch information
meuppt committed Mar 23, 2021
1 parent 9c43116 commit 16ddff8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,39 @@ $rt = EasyPeasy::instance('timed', 60); // Instantiate the class and sets a cach
$er = new EasyPeasy(); // Fatal error


```

Ok, now let's try something more interesting. It's not rare at all to see situations in which devs use Singletons in tandem with some constraints checkings, in the same class. Starting with the obvious, what if we came up with a Singleton that JUST creates a single instance if requirements are met?

That's exactly what we can do with the Selecton:

```php
require __DIR__ . '/vendor/autoload.php';

class EasyPeasy {

use \TonTon\Selecton;

private function __construct() {

echo 'Easy Peasy!';
}

}

$constraints = [
'extensions' => [ // Extension names
'curl',
'mbstring'
],
'classes' => [ // Class names (with namespaces, if existent)
'Directory',
'IteratorIterator'
]
];

$rt = EasyPeasy::instance($constraints); // If any of the constraints is not met, throws an exception

$er = new EasyPeasy(); // Fatal error

```
2 changes: 1 addition & 1 deletion src/Selecton.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait Selecton {
*
* @return self
*/
final public static function instance(array $constraints)
final public static function instance(array $constraints = null)
{

$extensions = get_loaded_extensions();
Expand Down

0 comments on commit 16ddff8

Please sign in to comment.