Download the PHP package spatie/typed without Composer
On this page you can find all versions of the php package spatie/typed. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package typed
Short Description Improvements to PHP's type system in userland: generics, typed lists, tuples and structs
License MIT
Homepage https://github.com/spatie/typed
Informations about the package 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.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
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.
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:
Why did we build this?
PHP has a very weak type system. This is simultaneously a strength and a weakness. Weak type systems offer a very flexible development platform, while strong type systems can prevent certain bugs from happening at runtime.
In its current state, PHP's type system isn't ready for some of the features many want. Take, for example, a look at some RFC's proposing changes to the current type system.
- Generics: https://wiki.php.net/rfc/generics
- Typed properties: https://wiki.php.net/rfc/typed-properties
- Readonly properties: https://wiki.php.net/rfc/readonly_properties
Some of those are already declined because of runtime performance issues, or implementation difficulties. This package is a thought experiment of what we could do if those features are implemented in PHP, usable with native syntax.
For example, the following syntax would be much more preferable over how this package does it.
Anyways, it's stuff to think about. And maybe PHP's type system is fine as it is now? You can read more about type safety on my blog.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
Credits
- Brent Roose
- All Contributors
License
The MIT License (MIT). Please see License File for more information.