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 )

Lists

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.