You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since `callable` and `array` cant be used for function names, instead i have used `func` for callable and `arr` for array.
45
35
`keyset`, `vector` and `set` are just helper to help you create an array of keys ( `keyset = arr(array_keys($value))` ), an array of values ( `vector = arr(array_values($value))` ) and an array of unique values ( `set = arr(array_unique($value))` ).
46
36
47
-
###typed
48
-
`typed()` is a function to help you create a typed variables based on the type of the passed variable, currently it supports `string`, `int`, `float`, `bool`, `object` and `iterable`. it was suggected by @mikesherov on [twitter](https://twitter.com/mikesherov/status/1084512906388144128).
37
+
#### `typed`
38
+
`typed()` is a function to help you create a typed variables based on the type of the passed variable, currently it supports `string`, `int`, `float`, `bool`, `object` and `iterable`. it was suggested by @mikesherov on [twitter](https://twitter.com/mikesherov/status/1084512906388144128).
49
39
50
-
---
40
+
#### `purge`, `clean`, `delete` and `c`
41
+
As mentioned [here](https://twitter.com/drealecs/status/1084658093252849665) and [here](https://www.reddit.com/r/PHP/comments/afq3it/typed_variables_for_php_74/ee1belg), memory leak is a huge issue here, if a specific function created a typed variable, the variable will still exist inside the repository even after execution,
42
+
for this i made some helper functions that allow you to remove variables from the repository.
51
43
52
-
check this [twitter thread](https://twitter.com/dshafik/status/1084248443118219264).
44
+
-`Typed\purge()` will delete every reference registered in the repository, you can call this function at the end of every request/response cycle in a web application per example.
45
+
-`Typed\clean(string $namespace = 'default')` this function will delete every reference to a typed variable in a specific namespace as shown in the example above, its recommended to use a unique namespace every time you use typed variables and call `clean()` to ensure there's no memory leak.
46
+
-`Typed\delete(mixed $value, string $namespace = 'default')` this function will delete the last created reference to typed variable with the given value in a specific namespace. example :
47
+
```php
48
+
<?php declare(strict_types=1);
53
49
54
-
---
50
+
use Typed as t;
51
+
52
+
$name = &t\string('azjezz', 'data');
53
+
$age = &t\int(19, 'data');
55
54
56
-
### Compoer installation :
57
-
```console
58
-
soon
55
+
t\delete('azjezz', 'data');
56
+
57
+
/**
58
+
* the reference to the `$name` variable has been deleted.
59
+
* therefor we can assign any type to the variable `$name` now.
60
+
*/
61
+
$name = []; // works
59
62
```
63
+
-`Typed\c` this function accepted a callable that take no arguments, the callable will be called inside `c` ( container ) and all the assigned typed variables in the time of the call would be deleted afterward, this makes it easier to ensure that function calls don't cause any memory leak. example :
64
+
```php
65
+
<?php declare(strict_types=1);
66
+
67
+
use Typed as t;
68
+
use function Typed\c;
69
+
70
+
/**
71
+
* all assigned variables inside the callable will be destroyed after execution.
72
+
*/
73
+
c(function(): void {
74
+
$name = &t\string('saif eddin');
75
+
$age = &t\int(5);
76
+
$arr = &t\arr([
77
+
'age' => $age,
78
+
'name' => $name
79
+
]);
80
+
});
81
+
82
+
```
83
+
84
+
#### More :
85
+
A cool PSR-15 Middleware to delete all typed variables created inside the next middleware/handler
86
+
87
+
```php
88
+
<?php declare(strict_types=1);
89
+
90
+
use Psr\Http\Server\MiddlewareInterface;
91
+
use Psr\Http\Server\RequestHandlerInterface;
92
+
use Psr\Http\Message\ServerRequestInterface;
93
+
use Psr\Http\Message\ResponseInterface;
94
+
use function Typed\c;
95
+
96
+
class TypedMiddleware implements MiddlewareInterface
97
+
{
98
+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
0 commit comments