Download the PHP package mrsuh/php-generics without Composer
On this page you can find all versions of the php package mrsuh/php-generics. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mrsuh/php-generics
More information about mrsuh/php-generics
Files in mrsuh/php-generics
Informations about the package php-generics
PHP generics written in PHP
Table of contents
- How it works
- Installation
- Monomorphization
- Type erasure
- Features
- Tests
How it works
In a nutshell:
- parse generics classes;
- generate concrete classes based on them (you can choose
monomorphization
ortype-erasure
); - autoload concrete classes instead of generics classes.
For example, you need to add several PHP files:
- generic class
Box
; - class
Usage
for use generic class; - script with composer autoload and
Usage
class.
src/Box.php
src/Usage.php
bin/test.php
Generate concrete classes from generic classes with composer dump-generics
command
What the composer dump-generics
command does?
- finds all generic uses in classes (
src/Usage.php
for example). - generates concrete classes from generic classes with unique names based on name and arguments of generic class.
- replaces generic class names to concrete class names in places of use.
In this case should be generated:
- 2 concrete classes of generics
BoxForInt
andBoxForString
; - 1 concrete class
Usage
with replaced generics class names to concrete class names.
Generate vendor/autoload.php with composer dump-autoload
command
Run bin/test.php script
Composer autoload first checks the "cache" directory and then the "src" directory to load the classes.
:blue_book: You can find repository with this example here.
Installation
Require
- PHP >= 7.4
- Composer (PSR-4 Autoload)
Install library
Add directory("cache/"
) to composer autoload PSR-4 for generated classes. It should be placed before the main directory.
composer.json
Monomorphization
A new class is generated for each generic argument combination.
Before monomorphization
:
After monomorphization
:
Command
Where in class can generics be used?
Where in generic class can parameters be used?
:blue_book: You can read more about monomorphization
here.
Type erasure
A new class is generated without generics arguments.
Before type erasure
:
After type erasure
:
Command
Where in class can generics be used?
Where in generic class can parameters be used?
:blue_book: You can read more about type-erasure
here.
Features
What syntax is used?
The RFC does not define a specific syntax so i took this one implemented by Nikita Popov
Syntax example:
Syntax problems
I had to upgrade nikic/php-parser for parse code with new syntax.
You can see here the grammar changes that had to be made for support generics.
Parser use PHP implementation of YACC.
The YACC(LALR) algorithm and current PHP syntax make it impossible to describe the full syntax of generics due to collisions.
Collision example:
Therefore, nested generics are not currently supported.
Parameter names have not special restrictions
Several generic parameters support
Default generic parameter support
How fast is it?
All concrete classes are pre-generated and can be cached(should not affect performance).
Generating many concrete classes should negatively impact performance when:
- resolves concrete classes;
- storing concrete classes in memory;
- type checking for each concrete class.
I think it's all individual for a specific case.
Doesn't work without composer autoload
Autoload magic of concrete classes works with composer autoload only.
Nothing will work because of syntax error if you include file by "require"
Reflection
PHP does type checks in runtime.
Therefore, all generics arguments must me available through reflection in runtime.
It can't be, because information about generics arguments is erased after concrete classes are generated.
Tests
How to run tests?
How to add test?
- Add directory 00-your-dir-name to ./tests/{monomorphic/type-erased}
- Generate output files and check it
All versions of php-generics with dependencies
composer-plugin-api Version ^1.0|^2.0
mrsuh/php-parser Version 95.4.0
symfony/console Version ^4.0|^5.0|^6.0
symfony/filesystem Version ^4.0|^5.0|^6.0
symfony/finder Version ^4.0|^5.0|^6.0