PHP generics written in PHP
Require
- PHP >= 7.4
- Composer (PSR-4 Autoload)
Table of contents
How it works
- parse generics classes;
- generate concrete classes based on them;
- say to "composer autoload" to load files from directory with generated classes first and then load classes from main directory.
Quick start
Install library
composer require mrsuh/php-generics
Add directory("cache/"
) to composer autoload PSR-4 for generated classes. It should be placed before the main directory.
composer.json
{
"autoload": {
"psr-4": {
"App\\": ["cache/","src/"]
}
}
}
Generate concrete classes from generic classes with composer dump-generics
command
composer dump-generics -vv
Generate vendor/autoload.php with composer dump-autoload
command
composer dump-autoload
Example
!! You can find repository with this example here.
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
<?php
namespace App;
class Box<T> {
private ?T $data = null;
public function set(T $data): void {
$this->data = $data;
}
public function get(): ?T {
return $this->data;
}
}
src/Usage.php
<?php
namespace App;
class Usage {
public function run(): void
{
$stringBox = new Box<string>();
$stringBox->set('cat');
var_dump($stringBox->get()); // string "cat"
$intBox = new Box<int>();
$intBox->set(1);
var_dump($intBox->get()); // integer 1
}
}
bin/test.php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use App\Usage;
$usage = new Usage();
$usage->run();
Generate concrete classes from generic classes with composer dump-generics
command
composer dump-generics -vv
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
composer dump-autoload
Run bin/test.php script
php bin/test.php
Composer autoload first checks the "cache" directory and then the "src" directory to load the classes.
Features
What syntax is used?
The RFC does not define a specific syntax so i took this one implemented by Nikita Popov
Syntax example:
<?php
namespace App;
class Generic<in T: Iface = int, out V: Iface = string> {
public function test(T $var): V {
}
}
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:
<?php
const FOO = 'FOO';
const BAR = 'BAR';
var_dump(new \DateTime<FOO,BAR>('now')); // is it generic?
var_dump( (new \DateTime < FOO) , ( BAR > 'now') ); // no, it is not
Therefore, nested generics are not currently supported.
<?php
namespace App;
class Usage {
public function run() {
$map = new Map<Key<int>, Value<string>>();//not supported
}
}
Parameter names have not special restrictions
<?php
namespace App;
class GenericClass<T, varType, myCoolLongParaterName> {
private T $var1;
private varType $var2;
private myCoolLongParaterName $var3;
}
Several generic parameters support
<?php
namespace App;
class Map<keyType, valueType> {
private array $map;
public function set(keyType $key, valueType $value): void {
$this->map[$key] = $value;
}
public function get(keyType $key): ?valueType {
return $this->map[$key] ?? null;
}
}
Default generic parameter support
<?php
namespace App;
class Map<keyType = string, valueType = int> {
private array $map = [];
public function set(keyType $key, valueType $value): void {
$this->map[$key] = $value;
}
public function get(keyType $key): ?valueType {
return $this->map[$key] ?? null;
}
}
<?php
namespace App;
class Usage {
public function run() {
$map = new Map<>();//be sure to add "<>"
$map->set('key', 1);
var_dump($map->get('key'));
}
}
Where in class can generics be used?
- extends
- implements
- trait use
- property type
- method argument type
- method return type
- instanceof
- new
- class constants
An example of class that uses generics:
<?php
namespace App;
use App\Entity\Cat;
use App\Entity\Bird;
use App\Entity\Dog;
class Test extends GenericClass<Cat> implements GenericInterface<Bird> {
use GenericTrait<Dog>;
private GenericClass<int>|GenericClass<Dog> $var;
public function test(GenericInterface<int>|GenericInterface<Dog> $var): GenericClass<string>|GenericClass<Bird> {
var_dump($var instanceof GenericInterface<int>);
var_dump(new GenericClass<int>::class);
var_dump(new GenericClass<array>::CONSTANT);
return new GenericClass<float>();
}
}
Where in generic class can parameters be used?
- extends
- implements
- trait use
- property type
- method argument type
- method return type
- instanceof
- new
- class constants
And example of generic class:
<?php
namespace App;
class Test<T,V> extends GenericClass<T> implements GenericInterface<V> {
use GenericTrait<T>;
use T;
private T|GenericClass<V> $var;
public function test(T|GenericInterface<V> $var): T|GenericClass<V> {
var_dump($var instanceof GenericInterface<V>);
var_dump($var instanceof T);
var_dump(new GenericClass<T>::class);
var_dump(T::class);
var_dump(new GenericClass<T>::CONSTANT);
var_dump(T::CONSTANT);
$obj1 = new T();
$obj2 = new GenericClass<V>();
return $obj2;
}
}
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?
php bin/test.php
How to add test?
- Add directory 00-your-dir-name to ./tests
- Generate output files and check it
php bin/generate.php tests/000-your-dir-name/input tests/000-your-dir-name/output 'Test\'