Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azjezz/typed
Typed variables for PHP 7.4+ ( don't use this please )
https://github.com/azjezz/typed
php php7 php74 psr-1 psr-2 psr-4 type-safety
Last synced: 4 months ago
JSON representation
Typed variables for PHP 7.4+ ( don't use this please )
- Host: GitHub
- URL: https://github.com/azjezz/typed
- Owner: azjezz
- License: mit
- Created: 2019-01-13T01:13:47.000Z (about 6 years ago)
- Default Branch: develop
- Last Pushed: 2020-06-18T23:08:07.000Z (over 4 years ago)
- Last Synced: 2024-09-29T20:01:20.606Z (4 months ago)
- Topics: php, php7, php74, psr-1, psr-2, psr-4, type-safety
- Language: PHP
- Homepage:
- Size: 21.5 KB
- Stars: 70
- Watchers: 10
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Typed
This library allows you to create typed variables in PHP 7.4 +
### Composer installation :
```console
$ composer require azjezz/typed
```### Usage Example :
```php
`func`, array => { `arr`, `keyset`, `vector`, `set` }
Since `callable` and `array` cant be used for function names, instead i have used `func` for callable and `arr` for array.
`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))` ).#### `typed`
`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).#### `purge`, `clean`, `delete` and `c`
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,
for this i made some helper functions that allow you to remove variables from the repository.- `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.
- `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.
- `Typed\filter(mixed $value, string $namespace = 'default')` this function will delete the every created reference to a typed variable with the given value in a specific namespace.
- `Typed\delete(mixed $value, string $namespace = 'default')` similar to what `Typed\filter` does, this function allows you to delete the last created reference to a typed variable with the given value in a specific namespace. example :
```php
$age,
'name' => $name
]);
});```
#### More :
A cool PSR-15 Middleware to delete all typed variables created inside the next middleware/handler```php
handle($request);
});
return $response;
}
}```
#### TODO :
- add `nullable*` functions.