Download the PHP package j0hnys/trident-typed without Composer
On this page you can find all versions of the php package j0hnys/trident-typed. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download j0hnys/trident-typed
More information about j0hnys/trident-typed
Files in j0hnys/trident-typed
Package trident-typed
Short Description Improvements to PHP's type system in userland: generics, typed lists, tuples and structs
License MIT
Homepage https://github.com/j0hnys/trident-typed
Informations about the package trident-typed
Improved PHP type system in userland
This package is a mere proof of concept about what's possible in PHP's userland to improve type checking. It adds support for type inference, generics, union types, typed lists, tuples and structs. Because all is done in userland, there are limitations on what syntax is possible.
Installation
You can install the package via composer:
Usage
Type inference
Both collections, tuples and structs support inferred types. This means that all examples are also possible, without manually specifying types. For example:
The following examples all show the manual type configuration. There are some cases where type inference falls short, and you have to fall back on manually defining them. You might also prefer the manual approach, for clarity's sake.
Note that type may be partially inferred. Some fields in tuples or structs may be type definitions, others may be real values. Uninitialised types will throw an error on read.
Typed lists and collections:
It's possible to directly initialise a collection with data after construction.
This package also provides some predefined lists, as shortcuts.
Maps:
It's possible to directly initialise a collection with data after construction.
Generics:
Generic types wrap around classes, allowing you to not creating a custom type for every class.
Tuples:
Like lists, a tuple can also be given some data after construction with the set
function.
Structs:
Nullable type
A nullable type can be defined in two, functionally identical, ways:
Union Type
A union type means a collection of multiple types.
Union types may also be nullable and contain generics.
What's not included:
- Proper syntax.
- IDE auto completion for generic types.
- Prevention of type casting between scalar types.
- Type hint generics in functions.
Creating your own types
The GenericType
or T::generic()
can be used to create structures of that type.
It is, however, also possible to create your own types without generics.
Let's take the example of Post
. The generic approach works without adding custom types.
The generic
part can be skipped if you create your own type.
Now you can use PostType
directly:
You're also free to extend the T
helper.
The Nullable
trait adds the following simple snippet,
so that the type can be made nullable when used.
Note: It's recommended to also implement
__toString
in your own type classes.
Extending data structures
You're free to extend the existing data structures. For example, you could make shorthand tuples like so: