Skip to content

Commit 16ddff8

Browse files
committed
First stable Selecton version.
1 parent 9c43116 commit 16ddff8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,39 @@ $rt = EasyPeasy::instance('timed', 60); // Instantiate the class and sets a cach
136136
$er = new EasyPeasy(); // Fatal error
137137

138138

139+
```
140+
141+
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?
142+
143+
That's exactly what we can do with the Selecton:
144+
145+
```php
146+
require __DIR__ . '/vendor/autoload.php';
147+
148+
class EasyPeasy {
149+
150+
use \TonTon\Selecton;
151+
152+
private function __construct() {
153+
154+
echo 'Easy Peasy!';
155+
}
156+
157+
}
158+
159+
$constraints = [
160+
'extensions' => [ // Extension names
161+
'curl',
162+
'mbstring'
163+
],
164+
'classes' => [ // Class names (with namespaces, if existent)
165+
'Directory',
166+
'IteratorIterator'
167+
]
168+
];
169+
170+
$rt = EasyPeasy::instance($constraints); // If any of the constraints is not met, throws an exception
171+
172+
$er = new EasyPeasy(); // Fatal error
173+
139174
```

src/Selecton.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait Selecton {
1515
*
1616
* @return self
1717
*/
18-
final public static function instance(array $constraints)
18+
final public static function instance(array $constraints = null)
1919
{
2020

2121
$extensions = get_loaded_extensions();

0 commit comments

Comments
 (0)